0

这是我的错误

mouse_cat.c:20: error: array type has incomplete element type
mouse_cat.c:20: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant
mouse_cat.c:27: error: array type has incomplete element type
mouse_cat.c:27: error: expected ‘;’, ‘,’ or ‘)’ before numeric constant

这是源代码

void enlever(char terrain [ ][ ],int x,int y)
  {
    terrain[y][x]=' ';
  }

//********************************************//

 void ajouter(char terrain [ ][ ],int x ,int y,int flag)
  {
   if(flag) 
    terrain[y][x]='C';
   else
    terrain[y][x]='S';
  }

这是我的宣言

#define x 23
#define y 22 

 char terrain [y][x]; 

我使用 Gcc (linux)

4

2 回答 2

2

定义宏具有以下语法:

#define name replacer

编译的第一阶段,预处理器阶段处理所有所谓的预处理器指令(以#开头的行),包括这个。在这种情况下,它将所有出现的name替换为replacer。因此,您的函数看起来像void enlever(char** terrain,int 23, int 22)实际的编译器。此外,您可能有变量名称,例如,其中包含字母 x 或 y。那些也将被替换。

为了避免这种情况,编码标准建议用大写字母命名用#define 声明的常量。但这还不够,因为名称XY仍然可能作为变量或用户定义数据类型的名称出现,甚至在字符串中出现。所以你可以使用类似的东西:

#define TERRAIN_LENGTH 23
#define TERRAIN_WIDTH 22

不要忘记使用常量而不是魔法数字(如在声明中int terrain[22][23];)是一种很好的做法,因为它们使您的代码更易于理解和维护。

于 2013-11-11T20:50:10.833 回答
1

您应该将代码更改为:

#define TX 23
#define TY 22 

void enlever(char terrain [ ][TY],int x,int y)
 {
    terrain[y][x]=' ';
  }

//********************************************//

 void ajouter(char terrain [ ][TY],int x ,int y,int flag)
  {
   if(flag) 
    terrain[y][x]='C';
   else
    terrain[y][x]='S';
  }

问题 1:函数形式参数被替换为宏定义。

问题 2:必须给出数组参数的第二个和后续维度:

另请参阅:GCC:数组类型具有不完整的元素类型

于 2013-11-11T20:35:42.220 回答