C ++编译错误:对' printMaze(int const(*)[16],int,int)'的未定义引用和Id返回1退出状态

时间:2016-11-04 04:26:14

标签: c++ c++11

我已经回答了这个问题,现在我编辑这个问题以帮助其他人理解这个问题

出于某种原因,当我尝试编译程序时,编译器说错误未定义引用" 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 move\n";
   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.
}

1 个答案:

答案 0 :(得分:1)

这是你的声明:

void printMaze(const int maze[][16], int xLoc, int yLoc);

这是你的定义:

void printMaze( const char maze[][ 16 ], int xLoc, int yLoc)

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