C 变量的初始值设定项不完整
我正在尝试创建一个具有默认值的结构,如下所述: C 结构中的默认值。但是,我在头文件中有这个 C 代码:
/* tokens.h */
typedef struct {
char *ID;
char *KEY;
char *TYPE;
} tokens;
const struct tokens TOKENS_DFLT = {
"id",
"key",
"type"
};
我在第 7 行收到一个错误:
error: variable 'TOKENS_DFLT' has initializer but incomplete type
任何人都可以向我解释这个问题是什么以及如何修复它并在将来防止它发生吗?
I am trying to make a struct with a default value, as described here: Default values in a C Struct. However, I have this C code, inside a header file:
/* tokens.h */
typedef struct {
char *ID;
char *KEY;
char *TYPE;
} tokens;
const struct tokens TOKENS_DFLT = {
"id",
"key",
"type"
};
And I am getting an error on line 7 saying:
error: variable 'TOKENS_DFLT' has initializer but incomplete type
Can anyone please explain to me what this problem is and how I can fix it and prevent it in the future?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尚未定义
结构标记
。您定义了一个未命名的struct
,并同时将其typedef
编辑为类型名称tokens
。如果您已经定义了
then 您可以将常量声明为任一:
或者
按原样,您位于两个凳子之间。
You haven't defined
struct tokens
. You've defined an unnamedstruct
and simultaneouslytypedef
-ed it to the type nametokens
.If you had instead defined
Then you could declare your constant as either:
Or
As it is, you're between two stools.
这:
应该是:
因为您已经将名称
tokens
定义为struct tokens
。This:
should be:
Since you've defined the name
tokens
to meanstruct tokens
.