0

I've read tons of similar questions,but I still can not figure it out. Error I get in QT creator when I try to compile this is next: "Grid has not been declared",although I included the Grid class header in the bunny class which displays this error. Here are my codes:

(Error is produced in function line "void becomeVampire(Bunny *& , int &,Grid &);" ,which is fifth if you count from down to up, in first(BUNNY) class, and also in function "void convertNeighbourToVampire(Bunny * const, int &, Grid &);" which is first from down to up: Both of these functions use reference to Grid class as one of parameters)

#ifndef BUNNY_H
#define BUNNY_H

#include <conio.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <windows.h>


#include "Position.h"
#include "Grid.h"

int GetRandom(int x);

class Bunny:public Position
{

private:
    static const std::vector <std::string> F_names;             //all the female names
    static const std::vector <std::string> M_names;             //all the male names
    static const std::vector <std::string> colors;              //all the colors
    bool isMale= GetRandom(2)<1 ? true : false;                 //at creation,bunnies gender is randomly chose//50%chance
    bool isVampire= GetRandom(100)< 2 ? true : false;           //2% chance for birth of vamp bunny
    std::string name;
    std::string color;
    std::string sex;
    std::string vampire;
    int age;
    void Inner(int & TVamps);                                   //use this inside of constructors,assign all needed info to a bunny
    void half(Bunny *&,int &);                                  //halfs the population randomly
    void increaseAge();                                         //called by ageUp() function,increases the age of a particular bunny object
public:
    Bunny * Next;                                               //pointer to next bunny in linked list

    Bunny(int & TVamps);                                        //DEFAULT CONSTRUCTOR
    Bunny(std::string C,int & TVamps);                          //CHILDREN CONSTRUCTOR
    ~Bunny();                                                   //DESTRUCTOR

    //NEXT is self explanatory
    bool isMatureMale() const {if(  (this->age>2) &&(isMale) &&(!isVampire) ){return true;} return false;}
    bool isMatureFemale() const {if(  (age>2) &&( !isMale) &&(!isVampire) ){return true;} return false;}
    bool getIsMale() const {return isMale;}
    std::string getName() const {return name;}
    std::string getColor() const {return color;}
    std::string getSex()const {return sex;}
    std::string getVampire() const{return vampire;}
    bool getIsVampire() const {return isVampire;}
    int getAge() const {return age;}

    void makeVampire(int &);                                        //make certain bunny a vampire
    bool isEmpty(Bunny *) const;                                    //is bunny colony empty
    void ageUp(Bunny *);                                            //increase the populations age by 1
    void insertBunny(Bunny *,Bunny *&, int &,int &);                //add a child,parameters are: head pointer,reference of last pointer,and counter of bunnies and vampires
    void insertFirstBunny(Bunny *&, int &,int &);                   //insert a bunny with no parrent
    void printBunnies(Bunny * , int , int,int) const;               //prints out the whole colony info
    void removeBunny(Bunny *& ,int &, int &);                       //removes old bunnies


    **void becomeVampire(Bunny *& , int &,Grid &);                  //converts bunnies to vampires**
    void FoodShortage(Bunny *& ,int &);                             //kills half of bunnies randomly,takes pointer reference of head pointer as parameter
    void massRabitKull(Bunny *&,int &);                             //when "k" or "K" is pressed,calls the half() function
    bool isArrayOutOfBounds(int x, Bunny const * const temp);        // north-1   south-2   east-3  west-4
    void convertNeighbourToVampire(Bunny * const, int &, Grid &);

};

#endif // BUNNY_H

And here is the Grid class:

#ifndef GRID_H
#define GRID_H
#include "Position.h"
#include "Bunny.h"

class Grid
{
private:
    char GridField[80][80];
    void renewGrid();                                       //sets all grid positions to '.',called by constructor and updateTheGrid()
public:
    Grid();
    void updateTheGrid(Bunny * const);                      //sets all grid positions to new positions based on new bunny objects linked list
    void printTheGrid() const;                              //prints the grid to the new file
    char getGridField(int,int);                             //returns the character on certain position


};

#endif // GRID_H

and the last class, Position:

#define POSITION_H
#include "Grid.h"

class Position
{
private:
    int row,column;
    char sign;

public:
    Position();
    Position ( int parrentRow,int parrentColumn );
    int getRow() const ;
    int getColumn() const;
    char getSign() const;

};

#endif // POSITION_H
xpg94
  • 495
  • 1
  • 10
  • 26
  • 2
    `Bunny.h` includes `Grid.h`, `Grid.h` includes `Bunny.h`. This can't work. – juanchopanza Mar 12 '14 at 22:59
  • 1
    The `#include`s are inside guards, so that can work. What will cause a problem is the lack of forward declarations. –  Mar 12 '14 at 23:00
  • @JohnGaughan It is quite hard to make circular includes work. It may be possible, but it it too messed up to consider. – juanchopanza Mar 12 '14 at 23:03
  • @juanchopanza So which header should not include the other one? Whichever I delete,then I get another compilation error (since I use Bunny object as a parametar in Grid functions,and Grid object as parametar in Bunny functions) – xpg94 Mar 12 '14 at 23:05
  • You can get away with forward declaring `class Grid` in `Bunny.h` and `class Bunny` in `Grid.h`. Remove the both includes, but remember to add them to the `.cpp` files that require them. And search SO. This has been asked about three zillion times. – juanchopanza Mar 12 '14 at 23:07
  • possible duplicate of [Resolve circular dependencies in c++](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c) – juanchopanza Mar 12 '14 at 23:10
  • Thanks man, could you also tell me what is "SO"? I kind of don't understand that. :) – xpg94 Mar 12 '14 at 23:11

0 Answers0