C++;编译错误:数组的元素类型不完整';int[]和#x27; 我正在写康威的C++游戏。我得到一个编译时错误,这与我将二维数组传递给方法的方式有关: gameoflife.cpp:5:25: error: array has incomplete element type 'int []' void print_game(int game[][], int SIZE); gameoflife.cpp:6:23: error: array has incomplete element type 'int []' void run_game(int game[][], int SIZE); gameoflife.cpp:7:23: error: array has incomplete element type 'int []' void set_cell(int game[][], int i, int j, int next[][], int SIZE);

C++;编译错误:数组的元素类型不完整';int[]和#x27; 我正在写康威的C++游戏。我得到一个编译时错误,这与我将二维数组传递给方法的方式有关: gameoflife.cpp:5:25: error: array has incomplete element type 'int []' void print_game(int game[][], int SIZE); gameoflife.cpp:6:23: error: array has incomplete element type 'int []' void run_game(int game[][], int SIZE); gameoflife.cpp:7:23: error: array has incomplete element type 'int []' void set_cell(int game[][], int i, int j, int next[][], int SIZE);,c++,methods,multidimensional-array,C++,Methods,Multidimensional Array,等等 我的代码的开头是: void print_game(int game[][], int SIZE); void run_game(int game[][], int SIZE); void set_cell(int game[][], int i, int j, int next[][], int SIZE); 显然,问题从这里开始 在方法中传递二维数组有什么问题?我应该用**来代替吗 在方法中传递二维数组有什么问题?我应该改用**吗 不是真的-如果可能,您应该使用向量的std::vec

等等

我的代码的开头是:

void print_game(int game[][], int SIZE);
void run_game(int game[][], int SIZE);
void set_cell(int game[][], int i, int j, int next[][], int SIZE);
显然,问题从这里开始

在方法中传递二维数组有什么问题?我应该用**来代替吗

在方法中传递二维数组有什么问题?我应该改用
**

不是真的-如果可能,您应该使用向量的
std::vector
,如下所示:

#include <vector>
...
void print_game(std::vector<std::vector<int> > game) {
    ... // No need to pass the size
}
#包括
...
无效打印游戏(标准::矢量游戏){
…//无需通过该大小
}
传递内置2D数组需要将两个维度中的一个指定为常量,或者将数组分配为指针数组,然后将指针传递给指针(即
int**
)。这两种选择都不是最优的:第一种选择将数组限制在编译时的最大值,而第二种选择要求您进行相当数量的手动内存管理

在方法中传递二维数组有什么问题?我应该改用
**

不是真的-如果可能,您应该使用向量的
std::vector
,如下所示:

#include <vector>
...
void print_game(std::vector<std::vector<int> > game) {
    ... // No need to pass the size
}
#包括
...
无效打印游戏(标准::矢量游戏){
…//无需通过该大小
}

传递内置2D数组需要将两个维度中的一个指定为常量,或者将数组分配为指针数组,然后将指针传递给指针(即
int**
)。这两种选择都不是最优的:第一种选择将数组限制在编译时的最大值,而第二种选择则要求您进行大量的手动内存管理。

如果不首先指定数组的大小或将数组作为指针传递,则无法传递数组。你能做的最好的就是

array[][5] //doesn't HAVE to be five, just example value

高度推荐使用@ DasBink的路由,但是,使用二维数组应该避免C++。

< P>不能在不指定大小的情况下传递数组的数组,或者将它们作为指针传递。你能做的最好的就是

array[][5] //doesn't HAVE to be five, just example value

高度推荐采用@ DasBink的光路,但是,使用二维数组应该避免C++。

< P>这个

int game[][]
是不完整类型数组的声明,因为数组的元素(例如游戏[0])的大小未知。必须使用常量表达式指定除最左侧标注外的所有标注的尺寸

例如,如果将SIZE定义为常量,并且数组具有相等的维度,则可以编写示例

void print_game(int game[][SIZE], int SIZE);

等等。

这个

int game[][]
是不完整类型数组的声明,因为数组的元素(例如游戏[0])的大小未知。必须使用常量表达式指定除最左侧标注外的所有标注的尺寸

例如,如果将SIZE定义为常量,并且数组具有相等的维度,则可以编写示例

void print_game(int game[][SIZE], int SIZE);


等等。

@πάνταῥεῖ 嗯,不是真的。OP,推荐阅读:@jrok“不是真的”不完全是,是的。但归根结底是同样的原因。@πάνταῥεῖ 嗯,不是真的。OP,推荐阅读:@jrok“不是真的”不完全是,是的。但归根结底也是同样的原因。