错误:不允许不完整的类型

时间:2014-10-18 11:41:45

标签: c

.h

中的

typedef struct token_t TOKEN;
.c

中的

#include "token.h"

struct token_t
{
    char* start;
    int length;
    int type;
};
main.c 中的

#include "token.h"

int main ()
{
    TOKEN* tokens; // here: ok
    TOKEN token;   // here: Error: incomplete type is not allowed
    // ...
}

我在最后一行得到的错误:

  

错误:不允许不完整的类型

怎么了?

2 个答案:

答案 0 :(得分:2)

您需要将struct的定义移到头文件中:

/* token.h */

struct token_t
{
    char* start;
    int length;
    int type;
};

答案 1 :(得分:1)

在主模块中没有结构的定义。您必须将其包含在标头中,编译器不知道要为此定义分配多少内存

TOKEN token;

因为结构的大小未知。大小未知的类型是不完整类型。

例如,您可以在标题中写入

typedef struct token_t
{
    char* start;
    int length;
    int type;
} TOKEN;
相关问题