Unresolved External Symbol

Hey guys, I've been learning SFML and am getting an obnoxious linker error that I can't understand :/ I have a static vector in my CMoveableObject class and I'm getting unresolved external symbol errors whenever I try to access it outside of the class. My code can be found here: http://www.sfmluploads.org/index.php?page=view_file&file_id=23

Thank you!
999 times out of 1000 "unresolved external symbol" means you created a function prototype but never gave that function a body.

If you post the exact error(s) here i can tell you exactly which function(s) are the culprit. (don't paraphrase errors!)

(I can't compile it for myself now because I'm at work)
1>main.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class CMoveableObject *,class std::allocator<class CMoveableObject *> > CMoveableObject::objects" (?objects@CMoveableObject@@2V?$vector@PAVCMoveableObject@@V?$allocator@PAVCMoveableObject@@@std@@@std@@A)
1>Move.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class CMoveableObject *,class std::allocator<class CMoveableObject *> > CMoveableObject::objects" (?objects@CMoveableObject@@2V?$vector@PAVCMoveableObject@@V?$allocator@PAVCMoveableObject@@@std@@@std@@A)

The thing is, it's a static vector, not a function. The only times it's used is in two constructors and the Draw function.
Static members need to be instantiated.

Globally in your movableobject cpp file, you need to put this line:

1
2
3
4
std::vector<CMoveableObject*> CMovableObject::PAVCMoveableObject;

// did I get the name right?  PAVCMoveableObject?  Name it whatever you named it
//   in the class 
Thank you, that fixed the first of my linker errors. I am now getting this:
CMoveableObject@@2V?$vector@PAVCMoveableObject@@V?$allocator@PAVCMoveableObject@@@std@@@std@@A) already defined in main.obj

I guess it's a "multiply defined" error, so next time I have a chance I'll look around and find the second definition...
It could also be that you put that code in a header file that is included in multiple cpp files. In that case, the static declaration needs to be in only one cpp, so either put it in your class' related cpp file, or if you inline your class functions pick a cpp file and stick it there.
Last edited on
Yeah I just discovered that by accident! Good call man!
Topic archived. No new replies allowed.