繁体   English   中英

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

[英]error: array type has incomplete element type typedef

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

#include <stdio.h>
#include <stdlib.h>
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 < boardWidth; w++)
                        fscanf(filePath, "%d", &board[0][h][w]);
        while(requestedGenerations > 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  )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM