I have a class Phone, I want it, when created to add itself to a static collection of phones. So I have the collection:
static vector < class Phone* > instances;
And in the constructor I do this:
Phone::instances.push_back(this);
But the linker throws an unresolved external symbol, why is that? What am I doing wrong? I didn't find a similar question. Is it necessary to add the instance outside the constructor? Or do I have to have the collection on another class? Thank you very much.
You must declare static member outside your class.
In your header:
class Phone{
...
static vector < class Phone* > instances;
...
};
In your cpp you need to create the instance of it:
//on global or namespace scope
vector <Phone*> Phone::instances;