匿名联合只能有非静态数据成员GCC c ++ [英] anonymous union can only have non-static data members GCC c++

查看:1101
本文介绍了匿名联合只能有非静态数据成员GCC c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C代码和使用GCC编译器。

I have some C code and using the GCC compiler.

代码在匿名联合中有一些嵌套类型:

The code has some nested types inside an anonymous union:

struct ab {
    int a;
    int b;
    union {
        int *c;
        int *d;
        struct f {
           int *c;
           int *d;
        };
        struct e {
            int *c;
            int *d;
        };
    };
};

我遇到此错误:

Error: 'struct ab::<anonymous union>::f' invalid; an anonymous union 
can only have non-static data members.

有人可以进一步解释此错误发生的原因吗?

Can someone give further explanation why this error is happening?

推荐答案

当引用union成员时,它像一个struct成员。

When referencing the union member, it acts like a struct member. You have created an ambiguous situation for the compiler.

这是有关GCC规范的更多信息: http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

Here is more information on the GCC specifications: http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html#Unnamed-Fields

如何编译C代码与匿名结构/联合?

这篇关于匿名联合只能有非静态数据成员GCC c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆