为什么当我通过引用传递数组时,我会收到一个编译器错误,指出我的变量未定义
Why am I getting a Compiler error stating that my variables are undefined when I pass an array by reference?
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
任何帮助将不胜感激!
数组
是通过引用传递的(默认情况下),所以我认为你会在不需要数组之前在函数原型中找到"&"。
您可能希望看到此问题:如何将对二维数组的引用传递给函数
相关文章:
- C++,我收到一个无法理解的编译器错误
- 当类型适当的构造函数可用时,为什么一个编译器尝试使用已删除的副本构造函数
- 编译器在C++中调用另一个函数时,在参数中查找已删除的构造函数
- 为什么 MSVC C++编译器将一个简单的 Hello World 扩展为 4000 行汇编?
- 在另一个编译器中打印乱码的数组 |C++
- 一个C++编译器是为不同的平台(Linux,Windows等)多次编写的,这是真的吗?
- Qt 5测试版2,Qt Creator 2.6,MSVC错误:Qt Creaator需要一个编译器来构建.在工具包选项中
- 为什么当我通过引用传递数组时,我会收到一个编译器错误,指出我的变量未定义
- 我能否获得一个C++编译器在编译时实例化对象
- 我有编译器的情况。当我运行代码来去除一串特殊字符时,它在一个编译器中运行,但在另一个编译器中不运行?
- 我认为这是一个编译器错误,这不应该影响我的代码,但它是
- 一个编译器中的运算符优先级不同,为什么
- 有符号整数溢出为负数:这是一个编译器错误,还是我误解了优化
- Bjarne 对这个 ADL 示例的看法是错误的,还是我有一个编译器错误
- 为什么不可能构建一个编译器来确定C++函数是否会更改特定变量的值
- QT需要建立一个编译器
- 我需要破解一个c++编译器来发出和解释一个特定的类指针作为全局数组内的索引
- 为什么一个C程序编译并链接到一个C编译器,然后在运行时SIGILL
- sizeof可以应用于未捕获的变量的lambda内部吗?还是这是一个编译器错误?
- 为什么这是一个编译器错误?(g + +)