我知道这个问题令人难以置信地被冲了上去,问过一千次,但我无法想象我的生活。尝试使用我的RatInMaze.cpp文件中的特定函数使用我的main函数进行编译时,我不断收到此错误。 searchStackSmart和searchStack不会导致任何问题,但searchQueue会造成任何问题。有什么想法吗?我的makefile也可能有问题...... RatInMaze.cpp用-c选项编译,但是当我不注释队列方法时,main.cpp根本不会编译
一些终端输出:
Bretts-MacBook-Pro:HW4 make
g++ main.o Arraylist.o Myexception.cpp RatInMaze.cpp -o main
Undefined symbols for architecture x86_64:
"RatInMaze::searchQueue(int, int, int, int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1
Bretts-MacBook-Pro:HW4 g++ main.cpp
Undefined symbols for architecture x86_64:
"RatInMaze::searchQueue(int, int, int, int)", referenced from:
_main in cczPtT5h.o
"RatInMaze::load(char (*) [15], int, int)", referenced from:
_main in cczPtT5h.o
"RatInMaze::print(bool)", referenced from:
_main in cczPtT5h.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
RatInMaze.cpp
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include "RatInMaze.h"
#include "Queue.h"
#include "Stack.h"
using namespace std;
void RatInMaze::load(char [13][15], int dimX,int dimy)
{
}
void RatInMaze::print(bool found)
{
if(found)
cout<<"I found the exit! I traveled through "<<endl;//<<squaresTraveled<<" square(s) and the path contained "<< pathSize<<" square(s)"<<endl;
else
cout<<"Unable to find the exit."<<endl;//<<squaresTraveled<<" square(s)";
}
char map[13][15]={
'0','0','0','1','0','0','0','0','0','0','1','0','0','0','0',
'0','0','0','1','0','0','1','0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','1','1','1','1','1','1','1','1',
'0','0','0','1','1','1','0','0','1','0','0','1','0','0','0',
'0','0','0','0','0','1','1','0','0','1','0','0','1','0','0',
'1','1','0','0','0','1','1','0','0','1','0','0','0','0','0',
'0','1','1','0','0','1','1','0','0','1','0','0','0','0','0',
'0','0','1','0','0','1','1','0','0','1','0','0','0','0','0',
'0','1','1','0','0','1','0','0','0','0','0','0','0','0','0',
'0','0','1','0','0','0','1','1','1','1','1','1','1','1','1',
'0','0','1','0','1','0','0','0','0','0','0','0','0','0','0',
'0','0','1','0','1','0','0','0','1','0','0','0','0','0','0',
'0','0','0','0','1','0','0','1','0','0','0','0','0','0','0' };
/*
Dumb Rat
While not at cheese, try moving right, then down, then left, then up each time in that order, adding to the stack each move and changing letter to indicate how we got there
If run into a road block, pop that last move off the stack and move back one space based on its letter (R,D,L,U) and continue
Smart Rat
Just like dumb, but check direction dx=toX-x each time and choose move based on that current situation
SearchQueue
"Sprawling rats"
start at the point, stack->push coordinates into queue stack->push(x) stack->push(y)
checke every direction possible and change the letter RLUD to indicate where you came from and stack->push each one of those pairs to the queue
When at the end, trace back based on the RLUD until you get to where you started ('4')
*/
bool RatInMaze::isValidIndex(int x, int y)
{
// 12 x 14 array
if (x < 0 || y < 0 || x > 12 || y >14)
return false;
return true;
}
/*
SearchQueue
"Sprawling rats"
start at the point, stack->push coordinates into queue stack->push(x) stack->push(y)
checke every direction possible and change the letter RLUD to indicate where you came from
and stack->push each one of those pairs to the queue
When at the end, trace back based on the RLUD until you get to where you started ('4')
*/
bool searchQueue(int fromX, int fromY, int toX, int toY)
{
int currentX;
int currentY;
int traveled;
int thePath;
//Matrix is 12x14, check dimensions
if (fromX<0 || fromX>12 || fromY < 0 || fromY >14)
{
cout<<"Not valid starting point"<<endl;
return false;
}
else if (toX<0 || toX >12 || toY <0 || toY >14)
{
cout<<"Not a valid ending point";
return false;
}
//Valid index, we can proceed
//Create Queue, starting points assigned
Queue* ratQueue = new Queue();
int x = fromX;
int y = fromY;
ratQueue->push(x);
ratQueue->push(y);
bool found = false;
traveled=0;
thePath=0;
while (!found)
{
currentX=ratQueue->pop();
currentY=ratQueue->pop();
if (currentX==toX && currentY==toY)
{
found = true;
break;
}
//Check right move
if (map[currentX][currentY+1] < 1)
{
ratQueue->push(x);
ratQueue->push(y+1);
map[x][y+1]='R';
traveled++;
}
//Check down move
if (map[currentX+1][currentY] < 1)
{
ratQueue->push(x+1);
ratQueue->push(y);
map[x+1][y]='D';
traveled++;
}
//Check left move
if (map[currentX][currentY-1] < 1)
{
ratQueue->push(x);
ratQueue->push(y-1);
map[x][y-1]='L';
traveled++;
}
//Check up move
if (map[currentX-1][currentY] < 1)
{
ratQueue->push(x-1);
ratQueue->push(y);
map[x-1][y] = 'U';
traveled++;
}
} //End while loop
//Cheese is now found
x=toX;
y=toY;
while( !(x==fromX && y==fromY))
{
//Check if we came from left
if (map[x][y]=='R')
{
map[x][y]='2';
y=y-1;
thePath++;
}
//Check if we came from above
else if (map[x][y]=='D')
{
map[x][y]='2';
x=x-1;
thePath++;
}
//Check if we came from right
else if (map[x][y]=='L')
{
map[x][y]='2';
y=y+1;
thePath++;
}
//Check if we came from below
else if (map[x][y]=='U')
{
map[x][y]='2';
x=x+1;
thePath++;
}
}
//Clean up array, leaving only 1s for the walls and 2 to show our path traveled
for(int i=0; i <13; i++)
{
for( int j=0; i<15; j++)
{
if (map[i][j]!='1' && map[i][j]!='2')
map[i][j]=0;
}
}
cout<<"Found the cheese! We traveled "<<traveled<<" squares and the path took "<<thePath<<" squares"<<endl;
return true;
//print(found, traveled, thePath);
} //End searchQueue method
bool RatInMaze::searchStack(int fromX, int fromY, int toX, int toY)
{
int backX;
int backY;
int traveled;
int thePath;
int x = fromY;
int y = fromX;
int temp = toY;
toY=toX;
toX=temp;
//Matrix is 12x14, check dimensions
if (fromX<0 || fromX>12 || fromY < 0 || fromY >14)
{
cout<<"Not valid starting point"<<endl;
return false;
}
else if (toX<0 || toX >12 || toY <0 || toY >14)
{
cout<<"Not a valid ending pointz"<<endl;
return false;
}
Stack* ratStack = new Stack();
//throws error
//Create stack? //ATTENTION
// Load Map? //ATTENTION
//Set map variables
map[x][y]='S'; //Mark starting point
ratStack->push(y); //First coordinates in
ratStack->push(x); //the stack are starting points
for (int e=0; e<13; e++)
{
cout<<"\n";
for (int p=0; p<15; p++)
{
cout<<map[e][p]<<" ";
}
}
//int iterations;
//Load map? Attention
while ( !(x==toX && y==toY))
{
if (ratStack->size() < -1000)
{
for (int e=0; e<13; e++)
{
cout<<"\n";
for (int p=0; p<15; p++)
{
cout<<map[e][p]<<" ";
}
}
}
//cout<<"Size is: "<<ratStack->size()<<endl;
//cout<<"iteration: "<<iterations<<endl; iterations++;
//Try to move right
if(isValidIndex(x,y+1) && map[x][y+1] < 1)
{
//stack->Push coordinates onto stack
ratStack->push(y);
ratStack->push(x);
y=y+1;
map[x][y]='R';
traveled++;
}
//Try to move down
else if(isValidIndex(x+1,y) && map[x+1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x+1;
map[x][y]='D';
traveled++;
}
//Try move left
else if(isValidIndex(x,y-1) && map[x][y-1] < 1)
{
ratStack->push(y);
ratStack->push(x);
y=y-1;
map[x][y]='L';
traveled++;
}
//Try move up
else if(isValidIndex(x-1,y) && map[x-1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x-1;
map[x][y]='U';
traveled++;
}
//Else you can't make any move, must retrace steps
else
{
//Pop off last coordinates on stack to get the previous square
backX = ratStack->pop();
backY= ratStack->pop();
//If we came from right, go back left, block off this square from revisiting
if(map[backX][backY]=='R')
{
map[backX][backY]='2';
y=y-1;
traveled--;
}
//If we came from above, go back up, block off this square from revisiting
else if(map[backX][backY]=='D')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//If we came from left, go back right if we can, block off this square from revisiting
else if(map[backX][backY]=='L')
{
map[backX][backY]='2';
y=y+1;
traveled--;
}
//If we came from below, go back up, block off this square from revisiting
else if(map[backX][backY]=='U')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//return to top of loop and we are now at the spot we came from before running into jam
//try to move right, down, left, up again as usual
//if not, we pop off another set of coordinates and repeat until we can move
}
} //End while loop
if (x==toX && y==toY)
{
cout<<"We found the cheese! "<<"It took us "<<ratStack->size()<<"squares and we traveled through" << traveled<<endl;
return true;
}
else
return false;
} //End method
char RatInMaze::checkDirection(int x, int y, int toX, int toY)
{
// If down and right, return 0
// If down and left, return 1
// If up and right, return 2
// If up and left, return 3
// If just right return 4
// If just left, return 5
// If just up return 6
// If just down, return 7
// If coordinates, return 8
if(x==toX) // we are in the right row, need to go left or right
{
if(y==toY)
return '9';
else if (toY-y >0) //need to go right
return '5';
else
return '6'; //need to go left
}
else if(y==toY) // we are in the right column, need to go up or down
{
if(toX-x>0) // need to go down
return '8'; //go down
else
return '7'; //neeed to go up
}
else if(toX-x>0) //cheese is down
{
if(toY-y>0) //right
return '0'; //right and down
else //up
return '1'; //left and down
}
else if(toX-x <0) //up
{
if(toY-y>0) // right
return '3';
else
return '4';
}
} //end check method
bool RatInMaze::searchStackSmart(int fromX, int fromY, int toX, int toY)
{
//Matrix is 12x14, check dimensions
//Matrix is 12x14, check dimensions
int x = fromY;
int y = fromX;
int temp = toY;
toY=toX;
toX=temp;
if (fromX<0 || fromX>12 || fromY < 0 || fromY >14)
{
cout<<"Not valid starting point"<<endl;
return false;
}
else if (toX<0 || toX >12 || toY <0 || toY >14)
{
cout<<"Not a valid ending point"<<endl;
return false;
}
Stack* ratStack = new Stack();
int backX;
int backY;
int traveled=0;
ratStack->push(y);
ratStack->push(x);
bool found=false;
while(!found)
{
char check =
checkDirection(x,y,toX,toY);
if (check='8')
{
found = true;
break;
}
else if (check='0') //Down and right
{
//Try to move down then right
//Try to move right
if(isValidIndex(x,y+1) && map[x][y+1] < 1)
{
//stack->Push coordinates onto stack
ratStack->push(y);
ratStack->push(x);
y=y+1;
map[x][y]='R';
traveled++;
}
//Try to move down
if(isValidIndex(x+1,y) && map[x+1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x+1;
map[x][y]='D';
traveled++;
}
}
else if (check='1') //Down and left
{
//Try down
if(isValidIndex(x+1,y) && map[x+1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x+1;
map[x][y]='D';
traveled++;
}
//Try left
if(isValidIndex(x,y-1) && map[x][y-1] < 1)
{
ratStack->push(y);
ratStack->push(x);
y=y-1;
map[x][y]='L';
traveled++;
}
}
else if (check='2') //Up and right
{
//Try right
if(isValidIndex(x,y+1) && map[x][y+1] < 1)
{
//stack->Push coordinates onto stack
ratStack->push(y);
ratStack->push(x);
y=y+1;
map[x][y]='R';
traveled++;
}
//Try up
if(isValidIndex(x-1,y) && map[x-1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x-1;
map[x][y]='U';
traveled++;
}
}
else if (check='3') //Up and left
{
//Try up
if(isValidIndex(x-1,y) && map[x-1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x-1;
map[x][y]='U';
traveled++;
}
//Try left
if(isValidIndex(x,y-1) && map[x][y-1] < 1)
{
ratStack->push(y);
ratStack->push(x);
y=y-1;
map[x][y]='L';
traveled++;
}
}
else if (check='4') //Just right
{
//Try right
if(isValidIndex(x,y+1) && map[x][y+1] < 1)
{
//stack->Push coordinates onto stack
ratStack->push(y);
ratStack->push(x);
y=y+1;
map[x][y]='R';
traveled++;
}
// if can't go right, try to go up or down
//Try up
else if(isValidIndex(x-1,y) && map[x-1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x-1;
map[x][y]='U';
traveled++;
}
//Try down
else if(isValidIndex(x+1,y) && map[x+1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x+1;
map[x][y]='D';
traveled++;
}
else //can't move anywhere, movebackwards
{
//Pop off last coordinates on stack to get the previous square
backX = ratStack->pop();
backY= ratStack->pop();
//If we came from right, go back left, block off this square from revisiting
if(map[backX][backY]=='R')
{
map[backX][backY]='2';
y=y-1;
traveled--;
}
//If we came from above, go back up, block off this square from revisiting
else if(map[backX][backY]=='D')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//If we came from left, go back right if we can, block off this square from revisiting
else if(map[backX][backY]=='L')
{
map[backX][backY]='2';
y=y+1;
traveled--;
}
//If we came from below, go back up, block off this square from revisiting
else if(map[backX][backY]=='U')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//return to top of loop and we are now at the spot we came from before running into jam
//try to move right, down, left, up again as usual
//if not, we pop off another set of coordinates and repeat until we can move
} //ELSE
}//ORIGINAL ELSE IF
else if (check='5') //Just left
{
//Try left
if(isValidIndex(x,y-1) && map[x][y-1] < 1)
{
ratStack->push(y);
ratStack->push(x);
y=y-1;
map[x][y]='L';
traveled++;
}
// if can't go left, try to go up or down
//Try up
else if(isValidIndex(x-1,y) && map[x-1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x-1;
map[x][y]='U';
traveled++;
}
//Try down
else if(isValidIndex(x+1,y) && map[x+1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x+1;
map[x][y]='D';
traveled++;
}
else //can't move anywhere, movebackwards
{
//Pop off last coordinates on stack to get the previous square
backX = ratStack->pop();
backY= ratStack->pop();
//If we came from right, go back left, block off this square from revisiting
if(map[backX][backY]=='R')
{
map[backX][backY]='2';
y=y-1;
traveled--;
}
//If we came from above, go back up, block off this square from revisiting
else if(map[backX][backY]=='D')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//If we came from left, go back right if we can, block off this square from revisiting
else if(map[backX][backY]=='L')
{
map[backX][backY]='2';
y=y+1;
traveled--;
}
//If we came from below, go back up, block off this square from revisiting
else if(map[backX][backY]=='U')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//return to top of loop and we are now at the spot we came from before running into jam
//try to move right, down, left, up again as usual
//if not, we pop off another set of coordinates and repeat until we can move
} //ELSE
} //ORIGINAL ELSE IF
else if (check='6') //Just up
{
//Try up
if(isValidIndex(x-1,y) && map[x-1][y] < 1)
{
ratStack->push(y);
ratStack->push(x);
x=x-1;
map[x][y]='U';
traveled++;
}
//Try right
else if(isValidIndex(x,y+1) && map[x][y+1] < 1)
{
//stack->Push coordinates onto stack
ratStack->push(y);
ratStack->push(x);
y=y+1;
map[x][y]='R';
traveled++;
}
//Try left
else if(isValidIndex(x,y-1) && map[x][y-1] < 1)
{
ratStack->push(y);
ratStack->push(x);
y=y-1;
map[x][y]='L';
traveled++;
}
else //can't move anywhere, movebackwards
{
//Pop off last coordinates on stack to get the previous square
backX = ratStack->pop();
backY= ratStack->pop();
//If we came from right, go back left, block off this square from revisiting
if(map[backX][backY]=='R')
{
map[backX][backY]='2';
y=y-1;
traveled--;
}
//If we came from above, go back up, block off this square from revisiting
else if(map[backX][backY]=='D')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//If we came from left, go back right if we can, block off this square from revisiting
else if(map[backX][backY]=='L')
{
map[backX][backY]='2';
y=y+1;
traveled--;
}
//If we came from below, go back up, block off this square from revisiting
else if(map[backX][backY]=='U')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
} //ELSE
} //ORIGINAL ELSE IF
else if (check='7') //Just down
{
//TRY RIGHT
if(isValidIndex(x,y+1) && map[x][y+1] < 1)
{
//stack->Push coordinates onto stack
ratStack->push(y);
ratStack->push(x);
y=y+1;
map[x][y]='R';
traveled++;
}
//TRY LEFT
//Try left
else if(isValidIndex(x,y-1) && map[x][y-1] < 1)
{
ratStack->push(y);
ratStack->push(x);
y=y-1;
map[x][y]='L';
traveled++;
}
else //can't move anywhere, movebackwards
{
//Pop off last coordinates on stack to get the previous square
backX = ratStack->pop();
backY= ratStack->pop();
//If we came from right, go back left, block off this square from revisiting
if(map[backX][backY]=='R')
{
map[backX][backY]='2';
y=y-1;
traveled--;
}
//If we came from above, go back up, block off this square from revisiting
else if(map[backX][backY]=='D')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
//If we came from left, go back right if we can, block off this square from revisiting
else if(map[backX][backY]=='L')
{
map[backX][backY]='2';
y=y+1;
traveled--;
}
//If we came from below, go back up, block off this square from revisiting
else if(map[backX][backY]=='U')
{
map[backX][backY]='2';
x=x+1;
traveled--;
}
} //ELSE END
} //ORIGINAL ELSE IF END
}//End while
// Now we should be at the end
if (x==toX && y==toY)
{
cout<<"We found the cheese! "<<"It took us "<<ratStack->size()<<"squares and we traveled through" << traveled<<endl;
return true;
}
else
return false;
}//End method
Main.cpp的
#include <iostream>
#include "RatinMaze.h"
using namespace std;
void print_header (string h, int fromX,int fromY,int toX,int toY) {
cout << h << "from " << "(" << fromY << "," << fromX << ") to ("
<< toY << "," << toX << "):" << endl;
}
int main (){
RatInMaze* rim = new RatInMaze();
char maze[13][15]={
'0','0','0','1','0','0','0','0','0','0','1','0','0','0','0',
'0','0','0','1','0','0','1','0','0','0','0','0','0','0','0',
'0','0','0','0','0','0','0','1','1','1','1','1','1','1','1',
'0','0','0','1','1','1','0','0','1','0','0','1','0','0','0',
'0','0','0','0','0','1','1','0','0','1','0','0','1','0','0',
'1','1','0','0','0','1','1','0','0','1','0','0','0','0','0',
'0','1','1','0','0','1','1','0','0','1','0','0','0','0','0',
'0','0','1','0','0','1','1','0','0','1','0','0','0','0','0',
'0','1','1','0','0','1','0','0','0','0','0','0','0','0','0',
'0','0','1','0','0','0','1','1','1','1','1','1','1','1','1',
'0','0','1','0','1','0','0','0','0','0','0','0','0','0','0',
'0','0','1','0','1','0','0','0','1','0','0','0','0','0','0',
'0','0','0','0','1','0','0','1','0','0','0','0','0','0','0' };
rim->load(maze,13,15);
print_header("Queue search ", -1,1,10,10);
rim->print(rim->searchQueue(-1,1,10,10));
//This causes a problem^
rim->load(maze,13,15);
print_header("Stack search ", 0,0,41,1);
rim->print(rim->searchStack(0,0,41,1));
int fromX = 0;
int fromY = 7;
int toX = 14;
int toY = 6;
rim->load(maze,13,15);
print_header("Rat (stack) searching ",fromX,fromY,toX,toY);
rim->print(rim->searchStack(fromX,fromY,toX,toY));
rim->load(maze,13,15);
print_header("Multiple rats searching ",fromX,fromY,toX,toY);
rim->print(rim->searchQueue(fromX,fromY,toX,toY));
//This causes a problem^
rim->load(maze,13,15);
print_header("Smart rat searching ",fromX,fromY,toX,toY);
rim->print(rim->searchStackSmart(fromX,fromY,toX,toY));
}
Makefile
main: main.o Arraylist.o Myexception.o RatInMaze.o
g++ main.o Arraylist.o Myexception.cpp RatInMaze.cpp -o main
main.o: main.cpp
g++ -c main.cpp RatInMaze.cpp
Arraylist.o: Arraylist.h Arraylist.cpp
g++ -c Arraylist.cpp
Myexception.o: Myexception.cpp
g++ -c Myexception.cpp
RatInMaze.o: RatInMaze.cpp Stack.h Queue.h Stack.cpp Queue.cpp
g++ -c RatInMaze.cpp Stack.h Queue.h
Stack.o: Stack.h Stack.cpp
g++ -c Stack.h
Queue.o: Queue.h Queue.cpp
g++ -c Queue.h
clean:
rm main.o RatInMaze.o
tarfile:
tar cvf Brett_Lindenberg_78363841.tar main.cpp Arraylist.cpp Arraylist.h Myexception.cpp Myexception.h Chain.cpp Chain.h chainNode.cpp chainNode.h Linearlist.h Makefile
答案 0 :(得分:0)
你的问题很简单,你会感到很尴尬。
你定义了:
bool searchQueue(int fromX, int fromY, int toX, int toY)
您需要将其更改为:
bool RatInMaze::searchQueue(int fromX, int fromY, int toX, int toY)
现在,您已经声明了一个名为RatInMaze::searchQueue
的函数,但定义了一个名为::searchQueue
的函数(即,您定义的searchQueue
不是{的成员{1}})。