问题描述
我已经定义了这些结构。
struct http_req {
struct http_req_line rl;
}
struct http_req_line {
enum method {
OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
} method;
enum uri_type {
ASTERISK,ABSOLUTEURI,ABS_PATH,AUTHORITY
} uri_type;
union req_uri {
char asterisk[1];
char absoluteURI[256];
char abs_path[256];
char authority[256];
} req_uri;
char http_ver[16];
};
当我编译包含这个头文件的文件时(它自己编译得很好),gcc 给了我这个
request_types.h:2:23: error: field ‘rl’ has incomplete type
struct http_req_line rl;
但是将标题的第二行更改为
struct http_req_line *rl;
摆脱了这个错误。导致此错误的原因是什么,将该成员更改为指针是否真的可以解决问题,或者只是对编译器隐藏它?
解决方法
在编译器看到的地方:
struct http_req {
struct http_req_line rl;
}
没有关于 struct http_req_line
是什么的信息。您需要 struct http_req
的结构定义出现在 struct http_req_line
的定义之后。您可以使用指针(指向不完整的类型);你不能使用结构的副本。
参见 C11 标准 §6.7.2.1 Structure and union specifiers:
结构或联合不应包含不完整或函数类型的成员(因此,结构不应包含自身的实例,但可以包含指向自身实例的指针),...
,在您定义 struct http_req
时,struct http_req_line
未知。编译器不知道它有多大,也不知道它的对齐要求是什么,所以它不知道要为 rl
成员使用多少空间,也无法完成它对 struct http_req
的定义。
如果您想在 struct http_req_line
中嵌入 struct http_req
,请将 struct http_req_line
的定义移到 struct http_req
的定义之前。
如果将 rl
更改为指针,则该类型在 C 意义上不是不完整的,因为编译器将知道指针有多大以及它的对齐要求是什么。它不需要知道它指向的结构有多大,以便能够为指针 rl
规划空间。
您看到此错误是因为您试图在定义结构本身之前定义结构的实例。你需要先定义它,然后使用它:
struct http_req_line {
enum method {
OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT
} method;
enum uri_type {
ASTERISK,ABSOLUTEURI,ABS_PATH,AUTHORITY
} uri_type;
union req_uri {
char asterisk[1];
char absoluteURI[256];
char abs_path[256];
char authority[256];
} req_uri;
char http_ver[16];
};
struct http_req {
struct http_req_line rl;
};
将成员类型更改为指针有效的原因是您不需要拥有完整的结构定义来使用指向它的指针。当需要类型定义时,该指针将被取消引用。