c++ - "Invalid use of incomplete type" in basic Hunter/Prey exercise -


so current assignment make basic hunter , prey simulation, , after having several other issues professor advised put in main.cpp thing working now.

my current issue in creature::find() function it's claiming grid class incomplete though it's pre-declared @ top of file. initial thought place grid class before creature, causes lot more or same error (referring creature being incomplete) since grid 2d array of creature pointers. below relevant bits of code, whole file can found on dropbox here.


class grid;  //*** error: forward declaration of 'class grid'  //other stuff...  class creature { public:     grid* thegrid;     coords position;     int stepbreed;     int stephunger;     char face;     bool hasmoved;      creature(grid* _grid, coords _position, char _face)  //constructor     {         thegrid = _grid;         position = _position;         face = _face;         stepbreed = stephunger = 0;         hasmoved = false;     }      vector<coords> find(char thisface)  //returns list of coords prey on them     {         vector<coords> result;         for(int = position.x-1; <= position.x+1; i++)             for(int j = position.y-1; j <= position.y+1; j++)             {                 coords temp(i,j);                 if(thegrid->occupant(temp) == thisface) //*** error: invalid use of incomplete type 'class grid'                     result.push_back(temp);             }         return result;     }      virtual coords move() = 0;  //allows creature type define it's own movement     virtual coords breed() = 0;  //allows creature type define it's own breeding     virtual bool starve() = 0;  //allows creature type starve of own accord };  class grid { public:     creature* grid[max_x][max_y];      grid() //initalizes grid , spawns random creatures     {         cout<<endl<<"grid init"<<endl;         for(int = 0; < max_x; i++)             for(int j = 0; j < max_y; j++)                 grid[i][j] = null;     }      void move() //tells each creature on grid move     {         cout<<endl<<"---  grid::move() top  ---"<<endl<<endl;         resetmoved();          for(int = 0; < max_x; i++)             for(int j = 0; j < max_y; j++)                 if(grid[i][j])                     grid[i][j]->move();         cout<<endl<<"---  grid::move() bottom  ---"<<endl<<endl;     }      void breed() //tells each creature breed (if can)     {      }      void kill()  //tells each creature die (if it's old)     {      }      char** snapshot()  //creates char array "snapshot" of board     {         char** result = new char*[max_x];         for(int = 0; < max_x; i++)         {             result[i] = new char[max_y];             for(int j = 0; j < max_y; j++)             {                 result[i][j] = occupant(coords(i, j));             }         }         return result;     }      creature* get(coords here)  //returns pointer object @ specified position     {         return grid[here.x][here.y];     }      char occupant(coords here) //returns character of specified position     {         if(!get(here))             return face_empty;         return get(here)->face;     }      void clear(coords here)  //deletes object @ specified position     {         cout<<endl<<"---  grid::clear() top  ---"<<endl<<endl;          if(!get(here))         {             cout<<"   inside if"<<endl;             delete get(here);         }         cout<<"   outside if"<<endl;         grid[here.x][here.y] = null;          cout<<endl<<"---  grid::clear() bottom  ---"<<endl<<endl;     }      void resetmoved()     {         for(int = 0; < max_x; i++)             for(int j = 0; j < max_y; j++)                 if(grid[i][j])                     grid[i][j]->hasmoved = false;     } }; 

edit: preview , markup toolbar isn't working reason.

you have circular dependency (i seem remember had question before). putting things in same file doesn't solve problem (although perhaps helps see problem little more clearly). have order things correctly every function defined after classes needs.

in case this

class grid;  class creature { public:     grid* thegrid;     ...     vector<coords> find(char thisface);     ... };  class grid { public:     creature* grid[max_x][max_y];     ... };  vector<coords> creature::find(char thisface) {    ... } 

creature::find needs both creature class (obviously) , grid class, must go after both classes have been defined.

if end putting creature::find definition in header file must add inline keyword, otherwise you'll multiple definitions.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -