为什么当我通过引用传递数组时,我会收到一个编译器错误,指出我的变量未定义

Why am I getting a Compiler error stating that my variables are undefined when I pass an array by reference?

本文关键字:一个 编译器 变量 未定义 我的 错误 引用 为什么 数组      更新时间:2023-10-16
include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int Rows = 5;
const int Cols = 5;
enum Minesweeper { Mine = '@', Blank = '*', Loss = 'X'};
void StudentInfo ( );
void Information (int mines);
void make_board (Minesweeper Board [][Cols], int Rows);
void place_mines (int& x, int& y, int mines, Minesweeper& Board [][Cols]); /* I pass the initialized Board array into another function that will place new characters in the array */
int main ( )
{
        int mines = 0;
        int x = 0;
        int y = 0;
        StudentInfo ( );
        Minesweeper Board [Rows][Cols];
        cout << "       Enter amount of mines (5 - 10): ";
        cin  >> mines;
        Information (mines);
        cout << "Preparing Board... n";
        cout << endl;
        make_board (Board, Rows);

运行此函数后,板 [行][列] 应为"*"

        place_mines (x, y, mines, Board);

此函数用于将数组中随机位置中的一些"*"替换为"@"

        return 0;
}

我正在尝试通过引用将已经初始化的 Board 传递到另一个函数中,以便它打印出一个新字符。

 void place_mines (int& x, int& y, int mines, Minesweeper& Board [][Cols]) /* In this function is states that 'mines' and 'Board' are not  declared in the scope */
{
        srand (time (0));
        for (int k = 0; k < mines; k++)
        {
                int x = (rand ( ) % 5);
                int y = (rand ( ) % 5);
                Board [x][y] = Mine;
                cout << static_cast<char> (Board [x][y]);
        }
        return;
}
void make_board (Minesweeper Board [][Cols], int Rows)
{
        int i = 0;
        int j = 0;
        for (int i = 0; i < Rows; i++)
        {
                for (int j = 0; j < Cols; j++)
                {
                                Board [i][j] = Blank;
                                cout << static_cast<char> (Board [i][j]) << ' ';
                }
        cout << endl;
        }
        Board [Rows][Cols] = Board [i][j];
        return;
}

这是我不断从编译器收到的错误

Minesweeper.cpp:13:72: error: declaration of ‘Board’ as array of references
Minesweeper.cpp:66:72: error: declaration of ‘Board’ as array of references
Minesweeper.cpp: In function ‘void place_mines(...)’:
Minesweeper.cpp:70:29: error: ‘mines’ was not declared in this scope
Minesweeper.cpp:75:17: error: ‘Board’ was not declared in this scope

任何帮助将不胜感激!

数组

是通过引用传递的(默认情况下),所以我认为你会在不需要数组之前在函数原型中找到"&"。

您可能希望看到此问题:如何将对二维数组的引用传递给函数

相关文章: