尝试将值分配给 typedef 结构变量的成员时出现问题

发布于 2024-10-31 05:50:42 字数 504 浏览 1 评论 0原文

我已经声明了这样的 typedef 结构:

typedef struct {
    u8  member_a;
    u32 member_d;
    u32 member_c;
    u16 member_d;
} __attribute__((packed)) fourmembers;

然后,我创建了一个名为“limp”的变量,其类型为“fourmembers”:

fourmembers limp;

接下来,我尝试为“fourmembers”变量的“member_a”成员分配一个值,例如结果

limp.member_a = 0x20;

是 GCC 给出了以下错误:

error: 'fourmembers' has no member named 'member_a'

任何人都可以告诉我我做错了什么吗?

I have declared a typedef structure like that:

typedef struct {
    u8  member_a;
    u32 member_d;
    u32 member_c;
    u16 member_d;
} __attribute__((packed)) fourmembers;

Then, I have created a variable named "limp" which is of type "fourmembers":

fourmembers limp;

Following, I've tried to assign a value to "member_a" member of the "fourmembers" variable like that:

limp.member_a = 0x20;

the result is that GCC gave the following error:

error: 'fourmembers' has no member named 'member_a'

Could anyone please advise me on what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

又爬满兰若 2024-11-07 05:50:43

您有两个名为 member_d 的成员变量。这可能对事情没有帮助。

一旦我改变了它,我就可以获得一个简短的代码片段来毫无问题地进行编译。因此,如果这不能解决您的问题,您需要发布一个小型的完整示例来演示该问题。

You have two member variables called member_d. That's probably not helping matters.

Once I alter that, I can get a short code snippet to compile without problems. So if this doesn't fix your problem, you'll need to post a small, complete example that demonstrates the issue.

酒儿 2024-11-07 05:50:43

您做错的不是查看第一个编译器错误 - 该错误告诉您为什么编译器无法创建fourmembers结构。此错误是 u8 不存在。

What you're doing wrong is not looking at the first compiler error - the one that tells you why the compiler couldn't create the fourmembers struct. This error would be that u8 doesn't exist.

囍笑 2024-11-07 05:50:43

嗯,既然您询问了有关您做错了什么的建议:

  1. 对固定大小类型使用非标准名称而不是标准名称 uint8_t uint16_t 等。这实际上是编译错误的来源。
  2. 使用__attribute__((packed))。忘记你曾经了解过它。总是错的。
  3. 没有给我们足够完整的代码片段来确定错误的原因。

Hmm, since you asked for advice on what you're doing wrong:

  1. Using nonstandard names for fixed-size types instead of the standard names uint8_t uint16_t etc. This is actually the source of your compile error.
  2. Using __attribute__((packed)). Forget you ever learned about it. It's always wrong.
  3. Not giving us a sufficiently complete code fragment to determine the cause of the error.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文