Question
C++ I am creating a simple 2D predator-Prey game and want to know why I am getting these 3 errors from my program (xcode)- Apple
C++ I am creating a simple 2D predator-Prey game and want to know why I am getting these 3 errors from my program (xcode)-
Apple Mach-0 Linker (Id) Error-
"ant::ant(board*, int, int)", referenced from:
"creature::getType()", referenced from:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Here is the code, thank you:
#include
#include
#include
#include
#include
using namespace std;
class board;
class creature;
class ant;
class doodlebug;
//Indicates the integer that represents each creature
const int antInd= 1;
const int doodlebugInd = 2;
int ants = 0;
int doodlebugs = 0;
int empty=0;
class board {
friend class creature;
friend class ant;
friend class doodlebug;
public:
board(){};
creature* grid[20][20];
void createGrid();
void initialize(int& ants, int& doodlebugs, int& empty);
void simTurn();
void printGrid();
protected:
board* thisBoard;
};
class creature: public board {
friend class board;
public:
creature(): turnsWithoutFood(0), turnsSurvived(0){}
creature(board* boardptr,int x, int y) {};
void breed(int x, int y);
void starve(int x, int y);
int getType();
private:
int x,y;
int turnsWithoutFood;
int turnsSurvived;
};
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 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 breed(int x, int y);
void starve(int x, int y);
void eat() ;
};
void board::createGrid() {
srand(time(NULL));
for(int i=0;i
for(int j=0;j
grid[i][j]= NULL;
}
}
}
void board::initialize(int& ants, int& doodlebugs, int& empty) {
while(doodlebugs
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
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 board::printGrid() {
for(int i=0;i
for(int j=0;j
if(grid[i][j]->getType() == 1) {
cout
}
else if(grid[i][j]->getType() == 2) {
cout
}
else {
cout
}
}
}
}
void ant::breed( int x, int y) {
if(grid[x+1][y] == NULL) {
grid[x+1][y] = new ant(this,x+1,y);
ant *thisAnt = (ant *)grid[x+1][y];
thisAnt->turnsSurvived=0;
ants++;
}
else if(grid[x-1][y] == NULL) {
grid[x-1][y] = new ant(this,x-1,y);
ants++;
ant *thisAnt = (ant *)grid[x-1][y];
thisAnt->turnsSurvived=0;
}
else if(grid[x][y+1] == NULL) {
grid[x][y+1] = new ant(this,x,y+1);
ants++;
ant * thisAnt = (ant *)grid[x][y+1];
thisAnt->turnsSurvived=0;
}
else if(grid[x][y-1] == NULL) {
grid[x][y-1] = new ant(this,x,y-1);
ants++;
ant * thisAnt = (ant *)grid[x][y-1];
thisAnt->turnsSurvived=0;
}
}
void doodlebug::breed(int x, int y) {
if(grid[x+1][y]==NULL) {
grid[x+1][y] = new doodlebug(this,x+1,y);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x+1][y];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
else if(grid[x-1][y]==NULL) {
grid[x-1][y] = new doodlebug(this,x-1,y);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x-1][y];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
else if(grid[x][y+1] == NULL) {
grid[x][y+1] = new doodlebug(this,x,y+1);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x][y+1];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
else if(grid[x][y-1]== NULL) {
grid[x][y+1] = new doodlebug(this,x,y-1);
doodlebugs++;
doodlebug* thisDoodlebug = (doodlebug *)grid[x][y-1];
thisDoodlebug->turnsSurvived=0;
thisDoodlebug->turnsWithoutFood=0;
}
}
void doodlebug::starve(int x, int y) {
delete grid[x][y];
doodlebugs--;
empty++;
}
void board::simTurn() {
int counter=0;
int right = 1;
int left = 2;
int up = 3;
int down = 4;
int x = 0;
int y = 0;
while(counter
if(grid[x][y]->getType() == antInd) {
ant* thisAnt = (ant* )grid[x][y];
int dir = rand()%4;
if(x==0) {
if(dir==2) {
dir=1;
}
}
if(y==0) {
if(dir==4) {
dir=3;
}
}
if(x==19) {
if(dir==1) {
dir=2;
}
}
if(y==19) {
if(dir==3) {
dir=4;
}
}
if(thisAnt->turnsSurvived==3) {
thisAnt->breed(x,y);
}
if(dir==right) {
if(grid[x+1][y] == NULL) {
thisAnt = (ant*)grid[x+1][y];
thisAnt->x+=1;
delete grid[x][y];
thisAnt->turnsSurvived++;
}
else if(grid[x+1][y]->getType() == doodlebugInd){
delete grid[x][y];
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
if(dir==left) {
if(grid[x-1][y] == NULL) {
thisAnt = (ant*)grid[x-1][y];
thisAnt->x-=1;
delete grid[x][y];
thisAnt->turnsSurvived++;
}
else if(grid[x-1][y]->getType() == doodlebugInd){
delete grid[x][y];
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
if(dir==up) {
if(grid[x][y+1] == NULL) {
thisAnt = (ant*)grid[x][y+1];
thisAnt->y+=1;
delete grid[x][y];
thisAnt->turnsSurvived++;
}
else if(grid[x][y+1]->getType() == doodlebugInd){
delete grid[x][y];
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
if(dir==down) {
if(grid[x][y-1] == NULL) {
thisAnt = (ant*)grid[x][y-1];
thisAnt->y-=1;
delete grid[x][y];
thisAnt->turnsSurvived++;
}
else if(grid[x][y-1]->getType() == doodlebugInd){
delete grid[x][y];
//ant is eaten
ants--;
thisAnt->turnsSurvived=0;
}
else {
thisAnt->turnsSurvived++;
}
}
// }
if(x==19) {
y++;
}
else {
x++;
}
counter++;
}
else if(grid[x][y]->getType() == doodlebugInd) {
doodlebug* thisDoodlebug = (doodlebug *)grid[x][y];
if(thisDoodlebug->turnsSurvived==8) {
thisDoodlebug->breed(x,y);
}
if(thisDoodlebug->turnsWithoutFood==3) {
thisDoodlebug->starve(x,y);
}
int dir = rand()%4;
if(x==0) {
if(dir==2) {
dir=1;
}
}
if(y==0) {
if(dir==4) {
dir=3;
}
}
if(x==19) {
if(dir==1) {
dir=2;
}
}
if(y==19) {
if(dir==3) {
dir=4;
}
}
if(dir==right) {
if(grid[x+1][y]== NULL) {
thisDoodlebug = (doodlebug *)grid[x][y];
thisDoodlebug->x+=1;
delete grid[x][y];
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
else if(grid[x+1][y]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
else if(dir==left) {
if(grid[x-1][y]== NULL) {
thisDoodlebug = (doodlebug *)grid[x][y];
thisDoodlebug->x-=1;
delete grid[x][y];
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
else if(grid[x-1][y]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
else if(dir==up) {
if(grid[x][y+1]== NULL) {
thisDoodlebug = (doodlebug *)grid[x][y];
thisDoodlebug->y+=1;
delete grid[x][y];
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
else if(grid[x][y+1]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
if(dir==down) {
if(grid[x][y-1]== NULL) {
thisDoodlebug = (doodlebug *)grid[x][y];
thisDoodlebug->y-=1;
delete grid[x][y];
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
else if(grid[x][y-1]->getType() == antInd) {
thisDoodlebug->turnsWithoutFood=0;
thisDoodlebug->turnsSurvived++;
}
else {
//do nothing
thisDoodlebug->turnsWithoutFood++;
thisDoodlebug->turnsSurvived++;
}
}
if(x==19) {
y++;
}
else {
x++;
}
counter++;
}
}
}
int main() {
srand(time(NULL));
board Board;
Board.initialize(ants,doodlebugs,empty);
Board.printGrid();
bool gameOver = false;
while(gameOver!=false) {
cout
string key;
cin>>key;
if(key=="") {
Board.simTurn();
Board.printGrid();
}
else if(key=="stop") {
gameOver=true;
}
}
}
Undefined symbols for architecture x86_64: "ant::ant (board*, int, int)", referenced from: board::initialize(int&, int&, int&) in main.o ant:: breed (int, int) in main.o "creature::getType()", referenced from: board::initialize(int&, int&, int&) in main.o board: printGrid() in main.o board:: simTurn() in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) "ant: ant(board, int, int)", referenced from: Board::initialize (int&, int&, int&) in main.o Ant:breed(int, int) in main.o 9"creature::getType)" referenced from Board:initialize(int&, int&, int&) in main.o Board::printGrid) in main.o Board::simTurn) in main.o Symbol(s) not found for architecture x86 64 Linker command failed with exit code 1 (use v to see invocation) Activity Log Complete 6/21/18, 1:51 AM 3 errors, 2 warnings
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