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

本文关键字:int 退出 状态 返回 引用 Id const 取消 错误 编译 定义      更新时间:2023-10-16

我已经得到了这个问题的答案,现在我编辑这个问题来帮助其他人理解这个问题

由于某些原因,当我尝试编译程序时,编译器说错误未定义引用'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)

更改其中一个以匹配另一个