c++windowsvisual-studio-2010linker

unresolved external symbol for structures in cpp?


I know most of time you get UNRESOLVED EXTERNAL SYMBOL for not including library or for not correctly defining function in class, but i get same error for structure. I have few files .cpp and .h in one of the .h i have defined

extern struct MyDataStruct StructData;

I include this .h in my .cpp file but i get

'struct MyDataStruct StructData' unresolved external symbol

I define this structure out of any class and i access it without any class prefix.


Solution

  • You need to actually define it in one and one only .cpp file ie allocate some space in the object file for it e.g.

    struct MyDataStruct StructData;
    

    Note in this case there is no extern.

    Whilst all other code accesses it through the declaration in the header which retains the extern - which tells the compiler and linker that somewhere else in the executable there is something to resolve the reference.