当函数 C++ 调用时,类 2D 数组将变为 arr []

Class 2D array turns to arr [] when called by function C++

本文关键字:arr 数组 2D C++ 函数 调用      更新时间:2023-10-16

我有一个大学作业,我对如何正确传递数组以防止数组作为单个数组而不是 2D 数组传递感到非常困惑。

我们将创建一个随机迷宫生成器,让我们也可以玩那个迷宫。我们正在使用专门的窗口代码来显示迷宫,但这不是问题所在,所以我把它排除在外我的讲师给了我们工作框架代码。我必须更改什么才能使其正常工作?

我们还没有学习动态内存位置或向量。我们必须使用数组。请帮忙!?

这是他的代码:我使用了相同的,只是添加了所有函数参数。不过我没有用"迷宫"改变任何东西

class MazeSquare
{
    public:
    bool leftWall, rightWall, bottomWall, topWall;
    bool visited;
    int steps;
    MazeSquare() // constructor
    {
        Initialise();
    }
    void Initialise(void) // reinitialise a square for a new maze
    {
        leftWall = true; // create the maze square with all the walls
        rightWall = true;
        bottomWall = true;
        topWall = true;
        visited = false; // the robot has not visited the square yet
        steps = 256; // greater than maximum possible number of steps
    }
};
// constants
const int MAZE_SIZE = 16;
// function prototypes
void CreateMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
void SolveMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
void RestartMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);
void MoveRobot(MazeSquare maze[MAZE_SIZE][MAZE_SIZE], int &x, int &y, Point click);
void DrawWindow(MazeSquare maze[MAZE_SIZE][MAZE_SIZE], int x, int y);
int ccc_win_main() // main function for a graphics program
{
    MazeSquare maze[MAZE_SIZE][MAZE_SIZE]; // maze design
    int x = 0, y = 0; // robot position
    bool exit = false; // flag to control end of program
    // initialise the random number generator
    srand((unsigned int)(time(NULL)));
    /* initialise the window coordinates here */
    CreateMaze(maze); // create a new maze
    DrawWindow(maze); // draw the image in the GUI window
    do
    {
        // get a mouse click
        Point click = cwin.get_mouse("Click a button or move the robot");
        // handle the different types of mouse clicks
        if (/* new button is clicked */)
        {
            CreateMaze(maze);
            x = 0;
            y = 0;
        }
        if (/* solve button is clicked */)
        {
            SolveMaze(maze);
        }
        if (/* restart button is clicked */)
        {
            RestartMaze(maze);
            x = 0;
            y = 0;
        }
        if (/* exit button is clicked */)
        {
            exit = true;
        }
        // handle robot moves
        if (/* maze is clicked */)
        {
            MoveRobot(maze, x, y);
        }
        DrawWindow(maze);
    } while (!exit);
    return 0;
}

困惑一 - 不能将数组传递给 C++ 中的函数。

困惑二 - 不能在C++中将数组声明为函数参数

困惑之三 - 2D 数组是单个数组,

2D 数组是数组的数组,因此它也是一个"单个数组"。我想我是说术语单个数组没有多大意义。

数组在C++是一个令人困惑的话题。你不能做你期望能够用它们做的所有事情。相反,一切都是用指针完成的。C++数组和指针之间的关系是另一个令人困惑的话题。你真的需要读一本书。任何具体问题,再问一遍。

但从好的方面来说,我认为你的代码没有任何特别的问题。你肯定不会像你担心的那样将单个数组传递给你的函数。

编辑:

也许我应该更清楚地说明这一点。关于第二点,这段代码

void CreateMaze(MazeSquare maze[MAZE_SIZE][MAZE_SIZE]);

当然看起来您正在声明一个带有数组参数的函数。但事实并非如此。相反,编译器获取代码并将其转换为使用指针的等效代码。

关于第一点,这段代码

CreateMaze(maze); // create a new maze

当然看起来你正在将数组传递给函数,但你又不是。给定代码,编译器传递指向迷宫数组的第一个元素的指针,它不会传递数组本身。