C++编译错误:取消定义对'printMaze(int const(*)[16],int,int)'的引用,Id 返回 1 退出状态
C++ compile error: undefine reference to 'printMaze(int const(*)[16],int,int)' and Id returned 1 exit status
我已经得到了这个问题的答案,现在我编辑这个问题来帮助其他人理解这个问题
由于某些原因,当我尝试编译程序时,编译器说错误未定义引用'printMaze(int const(*)[16],int,int)'并且Id返回1退出状态。谢谢你
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void printMaze(const int maze[][16], int xLoc, int yLoc);
int mazeTraverse(int maze[][16], int xLoc, int yLoc, int facing);
int main()
{
int maze[ 9 ][ 16 ] = {
{42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42},
{42, 0, 0, 0, -1, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 42},
{42, -1, -1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, 42},
{42, -1, -1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1, -1, -1, 42},
{42, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, 0, 0, 0, 0, 42},
{42, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, 0, 42},
{42, 90, -1, -1, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1, 0, 42},
{42, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1, -1, 80, 42},
{42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42}
};
int success = 0;
success = mazeTraverse(maze, 7, 14, 1); // Call function to move through the maze, assign returned value to success.
if (success == 1) // If success is equal to 1...
cout << "Congratulations! The maze has been solved.n"; // ...output congratulations...
else // ...else...
cout << "Unfortunately the maze has not been solved correctly.n"; // ...output failed message.
return 0; // End program.
}
// print the current state of the maze
void printMaze( const char maze[][ 16 ], int xLoc, int yLoc)
{
// nested for loops to iterate through maze
for ( int x = 0; x < 9; ++x )
{
for ( int y = 0; y < 16; ++y )
if ((x == xLoc) && (y == yLoc))
cout << 'X' << ' ';
else
cout << maze[ x ][ y ] << ' ';
cout << 'n';
} // end for
cout << "nHit return to see next moven";
cin.get();
} // end function printMaze
// Traverse through the maze one square at a time
int mazeTraverse(int maze[][16], int xLoc, int yLoc, int facing)
{
int success = 0;
maze[xLoc][yLoc] = 1; // Mark current location in the maze.
printMaze(maze, xLoc, yLoc); // Call function to display maze with current location marked.
while (success == 0) // While success is not equal to 0...
{
if ((xLoc == 6) && (yLoc == 1)) // If current location is the exit of the maze...
{
success = 1; // ...set success to 1.
}
else if (facing == 0) // Else if facing up...
{
if (maze[xLoc][yLoc+1] == 0) // ...check square to the right for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc+1, 1); // Move to the right.
}
else if (maze[xLoc-1][yLoc] == 0) // ...check square above for valid move...
{
success = mazeTraverse(maze, xLoc-1, yLoc, 0); // Move up.
}
else if (maze[xLoc][yLoc-1] == 0) // ...check square to the left for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc-1, 3); // Move to the left.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
else if (facing == 1) // If facing right...
{
if (maze[xLoc+1][yLoc] == 0) // ...check square below for valid move...
{
success = mazeTraverse(maze, xLoc+1, yLoc, 2); // Move down.
}
else if (maze[xLoc][yLoc+1] == 0) // ...check square to the right for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc+1, 1); // Move right.
}
else if (maze[xLoc-1][yLoc] == 0) // ...check square above for valid move...
{
success = mazeTraverse(maze, xLoc-1, yLoc, 0); // Move up.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
else if (facing == 2) // If facing down...
{
if (maze[xLoc][yLoc-1] == 0) // ...check square to the left for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc-1, 3); // Move to the left.
}
else if (maze[xLoc+1][yLoc] == 0) // ...check square below for valid move...
{
success = mazeTraverse(maze, xLoc+1, yLoc, 2); // Move down.
}
else if (maze[xLoc][yLoc+1] == 0) // ...check square to the right for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc+1, 1); // Move to the right.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
else if (facing == 3) // If facing left...
{
if (maze[xLoc-1][yLoc] == 0) // ...check square above for valid move...
{
success = mazeTraverse(maze, xLoc-1, yLoc, 0); // Move up.
}
else if (maze[xLoc][yLoc-1] == 0) // ...check square to the left for valid move...
{
success = mazeTraverse(maze, xLoc, yLoc-1, 3); // Move to the left.
}
else if (maze[xLoc+1][yLoc] == 0) // ...check square below for valid move...
{
success = mazeTraverse(maze, xLoc+1, yLoc, 2); // Move down.
}
else // If nowhere to go...
return 0; // ...close recursion to the previous junction.
}
} // ...end while loop.
return success; // Return value of success.
}
这是你的声明:
void printMaze(const int maze[][16], int xLoc, int yLoc);
,这是你的定义:
void printMaze( const char maze[][ 16 ], int xLoc, int yLoc)
更改其中一个以匹配另一个
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 在c++中访问int到类对象的映射时出错
- 如何在不返回 C++ 值的情况下退出 int 函数?
- 对"displayForStudent(int, int const*, double const*, int)"的未定义引用 collect2.exe:错误:ld 返回 1 个退出状态
- C++ 链接器错误未定义对"Suma(int, int)"的引用 ld 返回 1 个退出状态
- C++编译错误:取消定义对'printMaze(int const(*)[16],int,int)'的引用,Id 返回 1 退出状态
- For循环退出条件(size_t vs. int)