尝试将值分配给 typedef 结构变量的成员时出现问题
我已经声明了这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有两个名为
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.
您做错的不是查看第一个编译器错误 - 该错误告诉您为什么编译器无法创建
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 thatu8
doesn't exist.嗯,既然您询问了有关您做错了什么的建议:
uint8_t
uint16_t
等。这实际上是编译错误的来源。__attribute__((packed))
。忘记你曾经了解过它。总是错的。Hmm, since you asked for advice on what you're doing wrong:
uint8_t
uint16_t
etc. This is actually the source of your compile error.__attribute__((packed))
. Forget you ever learned about it. It's always wrong.