在 .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
怎么了?
你需要将 struct
到头文件中。
/* token.h */
struct token_t
{
char* start;
int length;
int type;
};
在主模块中,没有结构的定义。你必须在头文件中包含它,编译器不知道要为这个定义分配多少内存。
TOKEN token;
因为该结构的大小是未知的。一个未知大小的类型是一个不完整的类型。
例如,您可以在页眉中写道
typedef struct token_t
{
char* start;
int length;
int type;
} TOKEN;