c++ - Why must my global variable be declared as a pointer? -
when compiling c++ program receive no errors, within unordered_map hash function fails, attempting mod 0. (line 345 of hashtable_policy.h of stl)
i've found fix, don't know why i'm having problem begin with. struct looks this, (sorry specific code.)
struct player { private: entity& entity = entitymanager->create(); public: player() { entity.addcomponent(new positioncomponent(0, 0)); // add component uses unordered map. } }; player playerone; // error perpetuates through constructor.
however, if declare playerone pointer, so:
player* playerone;
and call:
playerone = new player();
i not have issues.
i've been searching - no success. doing wrong?
when use player global, you've no idea if entitymanager (presumably global) has been initialised yet - order of initialisation of globals isn't defined.
when use pointer , initialise new
(in main(), presume), globals have been created then, code works.
this highlights 1 of reasons why global variables bad idea.
Comments
Post a Comment