0

I have a class that houses a ID3D11Device member that I'd like to make static, since for now, I want the ID3D11Deviceto be accessed by multiple Entity class instances. The problem is, I'm unable to access the Direct3D class, housing the ID3D11Device static member outside of it's object without directly instantiating the Direct3D class.

Since multiple classes inherit from the Direct3D class (Which in turn inherits from a WindowsClass for hwnd detail), I'd like to keep it as a base class, as to keep access with all classes inheriting from Direct3D as this example proposes:

class Direct3D: WindowsClass{
public:
    Direct3D();
    ~Direct3D();

    static ID3D11Device* Device;
}

And an inherited class:

class EntityType: Direct3D{
public:
    EntityType();
    ~EntityType();

    void SomeDeviceDependentFunction();
}

Where the member is defined as:

void EntityType::SomeDeviceDependentFunction(){
//Perform some action calling Direct3D::Device. 
}

Note that the class Direct3D is defined in Direct3D.h, EntityType is defined in EntityType.h, and so on. So everything looks gravy in concept until I go to compile and receive the dreaded error:

Error 52 error LNK2001: unresolved external symbol "public: static struct ID3D11Device * Direct3d::Device" (?Device@Direct3d@@2PAUID3D11Device@@A) C:\Us...tions.obj Win32Project1

And:

Error 40 error LNK2005: "class std::vector<struct IndexVertexKey,class std::allocator<struct IndexVertexKey> > Geometry" (?Geometry@@3V?$vector@UIndexVertexKey@@V?$allocator@UIndexVertexKey@@@std@@@std@@A) already defined in D3DPipeline.obj C:\Use...nagement.obj Win32Project1

(Please note that the compile errors here do not relate directly to the examples given above, but are caused by them.)

To summarize, how can I control inherited classes where they are loosely coupled, (object to object) or where I can reference includes safely without dying of cross-references?

Rashid Ellis
  • 330
  • 3
  • 22

1 Answers1

1

The 1st error is a no definition error. You need to define the static member variable in implementation file (i.e. Direct3D.cpp),

ID3D11Device* Direct3D::Device = nullptr; // or other initial value

The 2nd error is a duplicate definition error, you should put the definition of the global variable in the implementation file, not the header file, which might be included multiple times and lead to a duplicate definition error.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Thanks I'll try that now. – Rashid Ellis Mar 04 '16 at 17:11
  • AH! That worked! Something so straight-forward! I literally read that in like three forms of c++ documentation and did not understand it. I can mark this as answer. – Rashid Ellis Mar 04 '16 at 17:25
  • I would like to use this comment to express additional gratitude: UGH, man! It's working like it was planned! Such a friggin' relief! – Rashid Ellis Mar 04 '16 at 17:51
  • Also, just to add, for the second compile error, (`error LNK2005...` ) there was a declaration `StandardKey Geometry;` declared in the global scope, outside of the class it was supposed to be declared in. – Rashid Ellis Mar 04 '16 at 21:45
  • @RashidEllis DId you solve the 2nd error? It's a duplicate definition error, you should put the definition into the implementation file, not the header file. BTW: `StandardKey Geometry;` is a definition, not declaration. – songyuanyao Mar 05 '16 at 01:26
  • Thanks for following up. The problem was resolved. Since StandardKey Geometry was placed outside the class, the struct was defined for every instance of the object, rather than an enclosed class instantiation. – Rashid Ellis Mar 05 '16 at 01:34