c++ - Odd behavior with classes in separate files -
this question has answer here:
- when can use forward declaration? 12 answers
i have classes this:
world.h:
#ifndef world_h_ #define world_h_ #include "gameobject.h" #include <vector> class world { public: std::vector<gameobject*> objects; world(); virtual ~world(); void add(gameobject*); void initialize(); void update(); void render(); }; #endif /* world_h_ */
gameobject.h:
#ifndef gameobject_h_ #define gameobject_h_ #include "util/point.h" #include "world.h" class gameobject { public: world *world; point *position; gameobject(); virtual ~gameobject(); virtual void update(); virtual void render(); }; #endif /* gameobject_h_ */
why give error:
"world.h, line 9 - 'gameobject' has not been declared " and
"world.h, line 13 - 'gameobject' not declared in scope"?
i using linux gcc 4.8.1 compiler.
you have circular include dependency. in case, can use forward declarations instead of includes:
#ifndef world_h_ #define world_h_ #include <vector> class gameobject; // forward declaration class world { // before }; #endif
and
#ifndef gameobject_h_ #define gameobject_h_ class world; class point; class gameobject { // before }; #endif
then, include headers in implementation files need them.
for more details, see when use forward declarations.
Comments
Post a Comment