我有一个类,其中包含ID3D11Device
成员,我想制作static
,因为现在我想让ID3D11Device
被多个Entity
类实例访问。问题是,我无法访问Direct3D
类,将静态成员放置在对象的外部,而无需直接实例化Direct3D
类。通过外部访问静态成员并通过继承C++
由于多个类从Direct3D
类(而这又从WindowsClass
继承了hwnd
细节)继承,我想保持它作为一个基类,以保持与所有类别的Direct3D
继承的这个例子访问提出:
class Direct3D: WindowsClass{
public:
Direct3D();
~Direct3D();
static ID3D11Device* Device;
}
和继承的类:
class EntityType: Direct3D{
public:
EntityType();
~EntityType();
void SomeDeviceDependentFunction();
}
当构件被定义为:
该类Direct3D
在
Direct3D.h
定义
void EntityType::SomeDeviceDependentFunction(){
//Perform some action calling Direct3D::Device.
}
注意,EntityType
在EntityType.h
定义,等等。所以一切看起来肉汁的概念,直到我去编译并收到可怕的错误:
Error 52 error LNK2001: unresolved external symbol "public: static struct ID3D11Device * Direct3d::Device" ([email protected]@@[email protected]@A) C:\Us...tions.obj Win32Project1
和:
Error 40 error LNK2005: "class std::vector<struct IndexVertexKey,class std::allocator<struct IndexVertexKey> > Geometry" ([email protected]@[email protected]@@[email protected]@@@[email protected]@@[email protected]@A) already defined in D3DPipeline.obj C:\Use...nagement.obj Win32Project1
(请注意,这里的编译错误不直接涉及到上面给出的例子,但是由它们造成的。)
总而言之,如何控制继承类,它们是松散耦合的(object to object)还是我可以在哪里参考包括安全而不会死亡的交叉引用?
必须在类定义之外定义'static'成员变量。 'ID3D11Device * Direct3D :: Device = <适当的初始值>;' –
谢谢,我正在尝试这个。 –