1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ This type of question is not specific to game development and should be asked on stack overflow instead. \$\endgroup\$ – Alexandre Vaillancourt Aug 29 '16 at 13:28
  • \$\begingroup\$ You should take a look into the singleton design pattern. IMHO it's a bit better than having this kind of global variable. \$\endgroup\$ – Alexandre Vaillancourt Aug 29 '16 at 13:32
1
\$\begingroup\$

Eventually i fixed this, by removing the include guards in 'Public.cpp', they were unnecessary because i already put them inside 'Public.h', and they were causing the errors !

\$\endgroup\$
  • \$\begingroup\$ And include guards should only be used in header files, they're the ones that can be included more than one time. \$\endgroup\$ – Alexandre Vaillancourt Aug 29 '16 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.