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

问题描述 投票:1回答:2

.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
    // ...
}

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

Error: incomplete type is not allowed

怎么了?

c struct declaration typedef incomplete-type
2个回答
5
投票

你需要将 struct 到头文件中。

/* token.h */

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

4
投票

在主模块中,没有结构的定义。你必须在头文件中包含它,编译器不知道要为这个定义分配多少内存。

TOKEN token;

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

例如,您可以在页眉中写道

typedef struct token_t
{
    char* start;
    int length;
    int type;
} TOKEN;
© www.soinside.com 2019 - 2024. All rights reserved.