Reputation: 51
I've seen other posts regarding vectors to an pointer of class objects. However, is it possible to populate this vector inside the same class' constructor with the "this" pointer? I'm getting a linker error. Am I way off base?:
// Object.h
class Object
{
....
static vector<Object*> gObjects;
}
// Object.cpp
Object::Object()
{
gObjects.push_back(this);
}
// Main.cpp
Object::gObjects.size()
Upvotes: 1
Views: 107
Reputation: 171263
is it possible to populate this vector inside the same class' constructor with the "this" pointer?
Yes, it's possible.
The linker error has nothing to do with pointers or putting this
in the vector, it's simply because you haven't defined the static member.
As explained at https://gcc.gnu.org/wiki/VerboseDiagnostics#missing_static_const_definition you need to declare and define static members.
To fix it just add this to Main.cpp
:
std::vector<Object*> Object::gObjects;
Upvotes: 1