c++ - error: expected ')' before '*' token in header -


i making program there hero has sword. have class both of those. in header error: expected ')' before '*' token on line sword(hero* h); in header of sword. here compete file (sword.h):

#ifndef sword_h #define sword_h  #include <hero.h>  class sword {     public:         sword(hero* h);         virtual ~sword(); };  #endif // sword_h 

hero.h in same directory hero.h, , i'm using code::blocks.

i've looked through other posts , couldn't find helped, given appreciated.

edit: here content of hero.h:

#ifndef hero_h #define hero_h  #include <string> #include <sdl.h> #include <sdl_image.h> #include <stdio.h>  #include <sword.h> #include <sprite.h> #include <window.h>  class hero : public sprite {     public:         hero(window* w);         void update();         void event(sdl_event e);         ~hero();     protected:     private:         bool up;         bool right;         bool left;         bool down;          window* window;         sword* sword; };  #endif // hero_h 

you cannot include sword.h hero.h , hero.h sword.h, inclusion chain has stop somewhere. can use forward declaration fix it:

//#include <hero.h> // remove  class hero; // forward declaration  class sword {     public:         sword(hero* h);         virtual ~sword(); }; 

this works because don't need definition of hero in sword.h. compiler needs know hero class.

you can same in hero.h: replace #include <sword.h> class sword;. can include files in corresponding .cpp files need definitions in order use classes.

rule of thumb: use forward declaration, unless whole header needs included.

further reading: when can use forward declaration?


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -