Question
C++- Why is it telling me in this code, that the variable grid (which is declared in class board) is undefined when I refer to
C++- Why is it telling me in this code, that the variable grid (which is declared in class board) is undefined when I refer to it in a function that is in class creature(creature is a friend class to board so should be able to use its variables) Here are the two errors I don't understand: The code is below them. Thank you
Error 1- On line 141 (the start of the void ant::breed function) I'm getting the error- 'Use of undeclared identifier-grid'
Error 2- On line 150( grid[x-1][y] = new ant(this,x-1,y);) I'm getting the error- No matching constructor for initialization of 'ant'
I don't understand why Im getting these two errors
Here is the code:
#include
#include
#include
#include
#include
using namespace std;
class board;
class creature;
class ant;
class doodlebug;
class invisible;
//Indicates the integer that represents each creature
const int antInd= 1;
const int doodlebugInd = 2;
const int emptyInd = 3;
int ants = 0;
int doodlebugs = 0;
int empty=0;
//int turnsSurvivedAnts = 0;
//int turnsSurvivedBugs = 0;
//int turnsWithoutFood = 0;
//int turnsStagnant=0;
class board {
friend class creature;
friend class ant;
friend class doodlebug;
friend class invisible;
public:
board();
creature* grid[20][20];
// int** creaturePtr;
// int** createGrid();
void createGrid();
void initialize(int& ants, int& doodlebugs, int& empty);
void simTurn();
void printGrid();
protected:
board* thisBoard;
};
class creature {
friend class board;
public:
creature(): turnsWithoutFood(0), turnsSurvived(0){}
creature(board* boardptr,int x, int y);
void move();
void breed(int x, int y);
void starve(int x, int y);
int getType();
private:
int x,y;
int turnsWithoutFood;
int turnsSurvived;
// creature getCreature();
// creature setCreature();
};
class ant: public creature {
friend class board;
public:
ant(): creature(), turnsSurvived(0){}
private:
int x,y;
ant(board* boardptr, int x,int y);
int getType() {
return 1;
}
int turnsWithoutFood;
int turnsSurvived;
void move();
void breed(int x, int y);
};
class doodlebug: public creature {
friend class board;
public:
doodlebug(): creature(), turnsWithoutFood(0), turnsSurvived(0){}
private:
int turnsWithoutFood;
int turnsSurvived;
doodlebug(board* boardptr, int x, int y);
int getType() {
return 2;
}
void move();
void breed(int x, int y);
void starve(int x, int y);
void eat();
};
class invisible {
friend class board;
private:
invisible(int x, int y);
int getType() {
return 3;
}
};
void board::createGrid() {
srand(time(NULL));
for(int i=0;i<20;i++) {
for(int j=0;j<20;j++) {
grid[i][j]= nullptr;
}
}
}
void board::initialize(int& ants, int& doodlebugs, int& empty) {
while(doodlebugs<100) {
int x = rand()%20;
int y= rand()%20;
creature* thisCreature = grid[x][y];
if(thisCreature->getType() != 2)
delete thisCreature;
thisCreature = new doodlebug(this,x,y);
doodlebugs++;
}
while(ants<5) {
int x = rand()%20;
int y= rand()%20;
creature* thisCreature = grid[x][y];
if(thisCreature == NULL)
delete thisCreature;
thisCreature = new ant(this,x,y);
ants++;
}
}
void ant::breed( int x, int y) {
if(grid[x+1][y]->getType() == 3) {
delete grid[x+1][y];
grid[x+1][y] = new ant(this,x+1,y);
ant thisAnt = grid[x+1][y];
thisAnt.turnsSurvived=0;
ants++;
}
else if(grid[x-1][y]->getType() == 3) {
delete grid[x-1][y];
grid[x-1][y] = new ant(this,x-1,y);
ants++;
ant thisAnt = grid[x-1][y];
thisAnt.turnsSurvived=0;
}
else if(grid[x][y+1]->getType() == 3) {
delete grid[x][y-1];
grid[x][y+1] = new ant(this,x,y+1);
ants++;
ant thisAnt = grid[x][y+1]
thisAnt.turnsSurvived=0;
}
else if(grid[x][y-1]->getType() == 3) {
else if(grid[x][y+1]->getType() == 3) {
delete grid[x][y-1];
grid[x][y-1] = new ant(this,x,y-1);
ants++;
ant thisAnt = grid[x][y-1]
thisAnt.turnsSurvived=0;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started