Error: " expected unqualified-id before 'int' ", array, argument, function,
Error: " expected unqualified-id before 'int' ", array, argument, function,
我是 C++ 的新手,目前正在通过认证学习这门语言。我之前只用过Python.
等语言
我发现了类似的帖子,但 none 可能与我的代码有关。
我有以下代码来创建一个十六进制游戏。我正在尝试有一个简单的功能来在玩家每次移动时显示棋盘。
我一开始尽量保持代码简单(限制使用指针和库)。
我有这个错误:
hex_game.cpp:9:47: 错误:在 'int' 之前需要不合格的 id
9 | void display_current_array(数组[大小][大小], 整数大小){
|
下面是我的代码,希望有人能帮忙:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <vector>
#include <array>
// a void function to display the array after every moove
void display_current_array(array[size][size], int size){
//s=arr.size();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout<<array[i][j]<<endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin>> size;
// create the array to represent the board
int array[size][size];
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
array[i][j]=0;
}
}
display_current_array(array, size);
}
编写此代码的更好方法是使用 STL 向量容器。如果你想用数组来做,你需要了解指针。
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
// a void function to display the array after every moove
void display_current_array(vector<vector<int>> &array, int size){ // & means pass-by-reference
//s=arr.size();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << array[i][j] << endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin >> size;
// create the array to represent the board
vector<vector<int>> array;
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i++)
{
vector<int> row;
for (int j = 0; j < size; j++)
{
row.push_back(0);
}
array.push_back(row);
}
display_current_array(array, size);
}
在这里,我使用 & 操作数通过引用传递向量。
void display_current_array(vector<vector<int>> &array, int size)
在标准C++中,数组的大小必须是编译时间常数。所以当你写道:
int size;
cin>> size;
int array[size][size]; //NOT STANDARD C++
声明array[size][size];
不是标准的c++。
第二个当你写道:
void display_current_array(array[size][size], int size){
//...
}
请注意,在函数 display_current_array
的第一个参数中,您 未指定数组包含的元素的类型 。所以这将导致您收到错误。
解决方案
避免这些并发症的更好方法是使用动态大小的容器,例如 std::vector
,如下所示:
#include <iostream>
#include <vector>
// a void function to display the vector after every moove
void display_current_array(const std::vector<std::vector<int>> &vec){//note the vector is passed by reference to avoid copying
for(auto &row: vec)
{
for(auto &col: row)
{
std::cout << col ;
}
std::cout<<std::endl;
}
}
int main(){
int size;
// ask the player to give a board size
std::cout << "What is the size of your Hex Board ? ";
std::cin>> size;
// create the 2D STD::VECTOR with size rows and size columns to represent the board
std::vector<std::vector<int>> vec(size, std::vector<int>(size));
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
//NO NEED TO INITIALIZE EACH INDIVIDUAL CELL IN THE 2D VECTOR SINCE THEY ARE ALREADY INITIALIED TO 0
display_current_array(vec);
}
注意:
- 我们不需要将向量的大小作为参数传递
- 矢量通过引用传递以避免复制
上面程序的输出可见here.
我是 C++ 的新手,目前正在通过认证学习这门语言。我之前只用过Python.
等语言我发现了类似的帖子,但 none 可能与我的代码有关。
我有以下代码来创建一个十六进制游戏。我正在尝试有一个简单的功能来在玩家每次移动时显示棋盘。
我一开始尽量保持代码简单(限制使用指针和库)。
我有这个错误: hex_game.cpp:9:47: 错误:在 'int' 之前需要不合格的 id 9 | void display_current_array(数组[大小][大小], 整数大小){ |
下面是我的代码,希望有人能帮忙:
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <vector>
#include <array>
// a void function to display the array after every moove
void display_current_array(array[size][size], int size){
//s=arr.size();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout<<array[i][j]<<endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin>> size;
// create the array to represent the board
int array[size][size];
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
array[i][j]=0;
}
}
display_current_array(array, size);
}
编写此代码的更好方法是使用 STL 向量容器。如果你想用数组来做,你需要了解指针。
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
// a void function to display the array after every moove
void display_current_array(vector<vector<int>> &array, int size){ // & means pass-by-reference
//s=arr.size();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout << array[i][j] << endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin >> size;
// create the array to represent the board
vector<vector<int>> array;
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i++)
{
vector<int> row;
for (int j = 0; j < size; j++)
{
row.push_back(0);
}
array.push_back(row);
}
display_current_array(array, size);
}
在这里,我使用 & 操作数通过引用传递向量。
void display_current_array(vector<vector<int>> &array, int size)
在标准C++中,数组的大小必须是编译时间常数。所以当你写道:
int size;
cin>> size;
int array[size][size]; //NOT STANDARD C++
声明array[size][size];
不是标准的c++。
第二个当你写道:
void display_current_array(array[size][size], int size){
//...
}
请注意,在函数 display_current_array
的第一个参数中,您 未指定数组包含的元素的类型 。所以这将导致您收到错误。
解决方案
避免这些并发症的更好方法是使用动态大小的容器,例如 std::vector
,如下所示:
#include <iostream>
#include <vector>
// a void function to display the vector after every moove
void display_current_array(const std::vector<std::vector<int>> &vec){//note the vector is passed by reference to avoid copying
for(auto &row: vec)
{
for(auto &col: row)
{
std::cout << col ;
}
std::cout<<std::endl;
}
}
int main(){
int size;
// ask the player to give a board size
std::cout << "What is the size of your Hex Board ? ";
std::cin>> size;
// create the 2D STD::VECTOR with size rows and size columns to represent the board
std::vector<std::vector<int>> vec(size, std::vector<int>(size));
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
//NO NEED TO INITIALIZE EACH INDIVIDUAL CELL IN THE 2D VECTOR SINCE THEY ARE ALREADY INITIALIED TO 0
display_current_array(vec);
}
注意:
- 我们不需要将向量的大小作为参数传递
- 矢量通过引用传递以避免复制
上面程序的输出可见here.