错误:数组类型具有不完整的元素类型typedef [英] error: array type has incomplete element type typedef

查看:323
本文介绍了错误:数组类型具有不完整的元素类型typedef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在快乐C.使生活的游戏,我被告知使用的typedef枚举并在可能的情况,所以我做了一个typedef 状态来的信号是否细胞 DEAD ALIVE
我做了持有的所有世代所有单板一个三维数组。
当我尝试使用数组作为函数的参数,它说错误:数组类型具有不完整的元素类型。难道我做的typedef错了吗?你不能有用户定义类型的数组?
这里是我的code:

i'm making a game of life in C. I was told to use typedef and enum wherever possible, so I made a typedef state to signal whether a cell is DEAD or ALIVE. I made a 3d array that holds all the boards in all generations. when I try to use the array as a parameter of a function, it says error: array type has incomplete element type. Did I do the typedef wrong? Can you not have arrays of user defined types? Here's my code:

#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");
        }
}

实际的错误消息:

Actual error messages:

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

任何帮助将是非常美联社preciated,谢谢。)

Any help would be much appreciated, thanks :).

推荐答案

您需要给规模持续在函数定义数组的两个维度,类似的东西,例如:

You need to give size to last two dimensions of array in function definition, something like that for example

     #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)
    {
       ....
    }

这是给系统提示如何计算,当您试图访问一个元素的索引。请记住,阵列(不管多少维度有)仅仅是一个连续的一块内存,所以当你索引阵列板[requestedGeneration] [H] [W] 编译器生成code一样,

That is to give the system a hint how to calculate the index of an element when you trying to access it. Remember that array (no matter how many dimensions it has) is just a continuous piece of memory, so when you index into your array board[requestedGeneration][h][w] compiler generates code like that

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

这篇关于错误:数组类型具有不完整的元素类型typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆