错误:数组类型具有不完整的元素类型typedef

我正在用C进行生活游戏。我被告知尽可能使用typedef和enum,所以我创建了一个typedef state来表示单元格是DEAD还是ALIVE 。 我制作了一个3Darrays,可以容纳所有世代的所有电路板。 当我尝试将数组用作函数的参数时,它表示error: array type has incomplete element type 。 我做的typedef错了吗? 你有没有用户定义类型的数组? 这是我的代码:

 #include  #include  typedef enum { DEAD, ALIVE } state; void nextGeneration(state[][][], int, int, int); int numberOfNeighbours(state[][][], int, int, int); void printGeneration(state[][][], int, int, int); int main(void) { FILE *filePath; int boardHeight; int boardWidth; int requestedGenerations; state board[100][10][10] = { DEAD }; int h; int w; if((filePath = fopen("file1", "r")) == NULL) { fprintf(stderr, "No such file.\n"); exit(1); } if(fscanf(filePath, "%d %d %d", &requestedGenerations, &boardHeight, &boardWidth) != 3) { fprintf(stderr, "File doesn't contain the number of requested generations or the board's size.\n"); exit(2); } for (h = 0; h < boardHeight; h++) for(w = 0; w  0) { nextGeneration(board, requestedGenerations--, boardHeight, boardWidth); printGeneration(board, requestedGenerations, boardHeight, boardWidth); } } void nextGeneration(state board[][][], int requestedGeneration, int boardHeight, int boardWidth) { int h; int w; int currentNumOfNeighbours; for(h = 0; h < boardHeight; h++) for(w = 0; w < boardHeight; w++) { currentNumOfNeighbours = numberOfNeighbours(board, requestedGeneration, h, w); if(board[requestedGeneration][h][w] == ALIVE) { if(currentNumOfNeighbours == 2 || currentNumOfNeighbours == 3) board[requestedGeneration + 1][h][w] == ALIVE; } else if(currentNumOfNeighbours == 3) board[requestedGeneration + 1][h][w] == ALIVE; } } } int numberOfNeighbours(state board[][][], int requestedGeneration, int h, int w) { int result = 0; if(board[requestedGeneration][h][w + 1]) result++; if(board[requestedGeneration][h][w - 1]) result++; if(board[requestedGeneration][h + 1][w]) result++; if(board[requestedGeneration][h - 1][w]) result++; if(board[requestedGeneration][h + 1][w + 1]) result++; if(board[requestedGeneration][h - 1][w + 1]) result++; if(board[requestedGeneration][h + 1][w - 1]) result++; if(board[requestedGeneration][h + 1][w - 1]) result++; return result; } void printGeneration(state board[][][], int requestedGeneration, int boardHeight, int boardWidth) { int h; int w; for(h = 0; h < boardHeight; h++) { for(w = 0; w < boardWidth; w++) printf("%d", board[requestedGeneration][h][w]); printf("\n"); } } 

实际错误消息:

 program1.c:4: error: array type has incomplete element type program1.c:5: error: array type has incomplete element type program1.c:6: error: array type has incomplete element type program1.c: In function 'main': program1.c:31: error: type of formal parameter 1 is incomplete program1.c:32: error: type of formal parameter 1 is incomplete program1.c: At top level: program1.c:35: error: array type has incomplete element type program1.c: In function 'nextGeneration': program1.c:43: error: type of formal parameter 1 is incomplete program1.c: At top level: program1.c:52: error: array type has incomplete element type program1.c:65: error: array type has incomplete element type 

任何帮助将非常感谢,谢谢:)。

你需要在函数定义中给出数组的最后两个维度的大小,例如类似的东西

  #define y 10 #define z 10 void nextGeneration(state board[][y][z], int requestedGeneration, int boardHeight, int boardWidth) { .... } int numberOfNeighbours(state board[][y][z], int requestedGeneration, int h, int w) { .... } 

这是为了给系统一个提示,当你试图访问它时如何计算元素的索引。 请记住,数组(无论它有多少维度)只是一块连续的内存,所以当你索引到你的数组board[requestedGeneration][h][w]编译生成这样的代码

 *(board + requestedGeneration * 10 * 10 + h * 10 + w )