C 变量的初始值设定项不完整

发布于 2024-10-03 00:19:49 字数 488 浏览 2 评论 0原文

我正在尝试创建一个具有默认值的结构,如下所述: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2024-10-10 00:19:49

您尚未定义结构标记。您定义了一个未命名的struct,并同时将其typedef编辑为类型名称tokens

如果您已经定义了

typedef struct tokens_ {
    char *ID;
    char *KEY;
    char *TYPE;
} tokens;

then 您可以将常量声明为任一

const struct tokens_ TOKENS_DFLT = { ... };

或者

const tokens TOKENS_DFLT = { ... };

按原样,您位于两个凳子之间。

You haven't defined struct tokens. You've defined an unnamed struct and simultaneously typedef-ed it to the type name tokens.

If you had instead defined

typedef struct tokens_ {
    char *ID;
    char *KEY;
    char *TYPE;
} tokens;

Then you could declare your constant as either:

const struct tokens_ TOKENS_DFLT = { ... };

Or

const tokens TOKENS_DFLT = { ... };

As it is, you're between two stools.

披肩女神 2024-10-10 00:19:49

这:

const struct tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

应该是:

const tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

因为您已经将名称 tokens 定义为 struct tokens

This:

const struct tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

should be:

const tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

Since you've defined the name tokens to mean struct tokens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文