I simply cannot seem to be able to make a static CArray and work with it.
Here's my code:
class WhiteBoard
{
public:
static CArray<WhiteBoard, WhiteBoard> test;
void tester()
{
test.Add(*this);
}
};
And upon calling the tester method, I get an unresolved external symbol. Full error code is:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: static class CArray<class WhiteBoard,class WhiteBoard> WhiteBoard::test" (?test@WhiteBoard@@2V?$CArray@VWhiteBoard@@V1@@@A) MFCApplication2 C:\Users\sw.eng\source\repos\MFCApplication2\MFCApplication2Dlg.obj 1
This is my first time working with CArray but I messed around with CArray of int and all worked alright.
Could it be that I'm trying to have a CArray of WhiteBoard inside of WhiteBoard? (I do need this functionality to keep track of all child objects made, which is why I do it)
Removing the static
keyword makes everything run. But then it's not a static member, and I need it to be one.
Anyways, all help would be incredible and very much appreciated.
EDIT 1:
I've made another class which I called BoardBoss. WhiteBoard inherits BoardBoss. CArray is now of BoardBoss type. CArray<BoardBoss, BoardBoss> test;
The issue persists.
CodePudding user response:
Static class members need to be defined. As posted, the WhiteBoard
class merely declares the identifier test
. That makes the compiler happy, but the linker fails, because it cannot find the referenced symbol when it is used.
To fix this you need to add the following to a compilation unit:
CArray<WhiteBoard, WhiteBoard> Whiteboard::test;