【问题标题】:Error: incomplete type is not allowed错误:不允许不完整的类型
【发布时间】:2014-10-18 11:41:45
【问题描述】:

.h中:

typedef struct token_t TOKEN;

.c中:

#include "token.h"

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

ma​​in.c

#include "token.h"

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

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

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

怎么了?

【问题讨论】:

  • 发布你正在编译的参数

标签: c struct declaration typedef incomplete-type


【解决方案1】:

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

TOKEN token;

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

例如你可以写在标题中

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

【讨论】:

    【解决方案2】:

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

    /* token.h */
    
    struct token_t
    {
        char* start;
        int length;
        int type;
    };
    

    【讨论】:

    • @Fabricio:如您所见,如果您使用不透明指针,则可以隐藏它。
    • 它需要在您创建结构实例的位置可见。否则编译器无法知道它的大小、字段等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2011-08-12
    • 2017-03-05
    • 1970-01-01
    相关资源
    最近更新 更多