C++ 使用命名空间中变量出问题。

天地蜉蝣 2011-08-27 12:49:08
tex.h:
#ifndef __TEX_H__
#define __TEX_H__
#include <d3dx9.h>
namespace Tex{
IDirect3DTexture9* Textures[7] = {0, 0, 0, 0, 0, 0, 0};// texture for each subset
void createTexture(IDirect3DDevice9* Device);
};
#endif

tex.cpp:
#include "tex.h"

void Tex::createTexture(IDirect3DDevice9* Device)
{
//...修改Textures[7]
}


cube.h
#ifndef __cubeH__
#define __cubeH__
#include <d3dx9.h>
#include <string>
#include <vector>

class Cube
{
public:
Cube(IDirect3DDevice9* device,D3DXVECTOR3 position, int tex[6]);
~Cube();
bool draw(D3DXMATRIX* world, D3DMATERIAL9* mtrl);
private:
IDirect3DDevice9* _device;
ID3DXMesh* _mesh;
static const DWORD _numSubsets;
std::vector<int> _tex;
};
#endif //__cubeH__

cube.cpp

#include "cube.h"
#include "vertex.h"
#include "tex.h"
#include "d3dUtility.h"

const DWORD Cube::_numSubsets = 6;
//extern IDirect3DTexture9* Textures[7];
Cube::Cube(IDirect3DDevice9* device,D3DXVECTOR3 position,int tex[6])
{ //...
//初始化成员变量,
}

Cube::~Cube()
{
d3d::Release<ID3DXMesh*>(_mesh);
}

bool Cube::draw(D3DXMATRIX* world, D3DMATERIAL9* mtrl)
{
if( world )
_device->SetTransform(D3DTS_WORLD, world);
if( mtrl )
_device->SetMaterial(mtrl);

for(int i = 0; i < _numSubsets; i++)
{
_device->SetTexture( 0, Tex::Textures[_tex[i]] );
//此处出现问题,我想用命名空间Tex中的变量Textures,却出现重定义的问题
_mesh->DrawSubset( i );
}
return true;
}

main.cpp:

#include "d3dUtility.h"
#include "vertex.h"
#include "cube.h"
#include "camera.h"
#include <iostream>
#include "tex.h"
bool Setup()
{
//.....
Tex::createTexture(Device);
//.....
}

bool Display(float timeDelta)
{
if( Device )
{
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xff0000ff, 1.0f, 0);
Device->BeginScene();
for(int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
for(int z=0;z<3;z++)
{
if(Box[x][y][z])
Box[x][y][z]->draw(&CubeWorldMatrix, 0);//调用draw
}
}
}
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}


1>正在链接...
1>main.obj : error LNK2005: "struct IDirect3DTexture9 * * Tex::Textures" (?Textures@Tex@@3PAPAUIDirect3DTexture9@@A) 已经在 cube.obj 中定义
1>tex.obj : error LNK2005: "struct IDirect3DTexture9 * * Tex::Textures" (?Textures@Tex@@3PAPAUIDirect3DTexture9@@A) 已经在 cube.obj 中定义
1>D:\project\D3DTest\Release\D3DTest.exe : fatal error LNK1169: 找到一个或多个多重定义的符号
...全文
200 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lidongdong_xynu 2012-11-19
  • 打赏
  • 举报
回复
能不能再解释的清楚些, 我也遇到了这个问题?
天地蜉蝣 2011-09-16
  • 打赏
  • 举报
回复
已经解决,问题在于namespace中的变量一般为声明,而定义的地方一般在cpp中。
snowraindy 2011-09-14
  • 打赏
  • 举报
回复
在不同的编译器里 可能不支持 namespace 就会出现上述错误 ,还有种情况就是楼上师兄说的这种
Sou2012 2011-08-30
  • 打赏
  • 举报
回复
重复定义了呢

8,305

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧