2014-10-18 88 views
0

.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

怎么了?

+0

发布与你正在编译 – danish 2014-10-18 11:47:17

回答

2

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

/* token.h */ 

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

嗯参数。 。我以为我应该隐藏结构实现... – Fabricio 2014-10-18 11:44:50

+1

@Fabricio:你可以隐藏它,如果你使用不透明的指针,如你所见。 – 2014-10-18 11:45:39

+1

它需要在您创建结构实例的位置可见。否则,编译器无法知道它的大小,字段等。 – NPE 2014-10-18 11:45:46

1

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

TOKEN token; 

由于结构的大小是未知的。未知大小的类型是不完整类型。

例如,你可以在头

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