Okay so i have an extern vector of pointer type 'Entity', and what i want to do is, when a new Entity type class gets constructed it gets pushed back in the vector of Entities, in C# this would be possible by passing 'this' as a parameter, but i'm unable to do that in c++ ! Here is my code:
'Public.h':
// other code
#ifndef Entity_h
#include "Entity.h"
extern vector <Entity*> AllEntities;
#endif
// other code
I've put the code in Include guards to not get in trouble, because i'm including 'Public.h' from 'Entity.cpp' and if i didn't have guards it would become a circular dependency !
'Public.cpp':
// other code
#ifndef Entity_h
vector <Entity*> AllEntities;
#endif
// other code
Everything fine, no errors!
'Entity.cpp':
#include "Public.h"
#include "Entity.h"
Entity::Entity()
{
// other code
AllEntities.push_back(this);
}
i'm trying to do this, but doing this causes me some linker errors:
Error 3 error LNK2005: "void __cdecl AddToAllENT(class Entity *)" (?AddToAllENT@@YAXPAVEntity@@@Z) already defined in Entity.obj C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\ECSystem\Public.obj ECSystem
Error 4 error LNK2001: unresolved external symbol "class std::vector<class Entity *,class std::allocator<class Entity *> > AllEntities" (?AllEntities@@3V?$vector@PAVEntity@@V?$allocator@PAVEntity@@@std@@@std@@A) C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\ECSystem\Entity.obj ECSystem
Error 5 error LNK1120: 1 unresolved externals C:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\Debug\ECSystem.exe 1 1 ECSystem
When i try to do this:
Entity::Entity()
{
// other code
AllEntities->push_back(this);
}
it gives me these errors!
Error 1 error C2819: type 'std::vector<Entity *,std::allocator<_Ty>>' does not have an overloaded member 'operator ->' c:\users\drin-_000\documents\visual studio 2013\projects\ecsystem\ecsystem\entity.cpp 9 1 ECSystem
Error 2 error C2232: '->std::vector<Entity *,std::allocator<_Ty>>::push_back' : left operand has 'class' type, use '.' c:\users\drin-_000\documents\visual studio 2013\projects\ecsystem\ecsystem\entity.cpp 9 1 ECSystem
3 IntelliSense: expression must have pointer type c:\Users\drin-_000\Documents\Visual Studio 2013\Projects\ECSystem\ECSystem\Entity.cpp 9 2 ECSystem