简体   繁体   English

在结构中使用枚举值

[英]Using value of enum within struct

I have the following class: 我有以下课程:

class MyClass 
{
    public:

        enum myEnum
        {
            a = 0b0,
            b = 0b1,
        };

        union myUnion
        {
            uint32_t all;

            struct myStruct
            {
                uint32_t start     : 0b1;
                uint32_t enumValue : myEnum::a;
            } bits;
        };

    ...

};

I am simply unsure how to use the value of myEnum::a within the struct . 我只是不确定如何在struct使用myEnum::a的值。 I've tried a number of ways of referencing the value to no avail. 我尝试了多种引用该值的方法。 How would I achieve this? 我将如何实现?

Here's the error: Error: Name followed by "::" must be a class or namespace name in "MAX1300BEUG/MAX1300.h", Line: 50, Col: 37 错误信息: Error: Name followed by "::" must be a class or namespace name in "MAX1300BEUG/MAX1300.h", Line: 50, Col: 37

Thanks, Adam 谢谢亚当

Enums don't have a scope. 枚举没有作用域。 Therefore applying the scope resolution operator :: to myEnum is wrong. 因此,将范围解析运算符::应用于myEnum是错误的。

The enum values are declared in the enclosing namespace, so within myStruct , which is within the same scope as myEnum , you can refer to its values directly using unqualified lookup: a , b . 枚举值在封闭的名称空间中声明,因此在myStruct (与myEnum处于同一范围内)内,您可以使用不限定条件的查找直接引用其值: ab

Outside of MyClass , it can be resolved with MyClass::a . MyClass之外,可以使用MyClass::a解决。


PS The value of a is 0. A zero width bitfield may not be named. PS a值为0。零宽度位字段可能未命名。 There is some sort of disconnect between what you're trying to do, and what you're trying to achieve by doing it. 您尝试执行的操作与通过此操作要实现的操作之间存在某种脱节。

myEnum::a is defined as 0 . myEnum::a定义为0 Zero width bit fields are not allowed in this context. 在这种情况下,不允许使用零宽度的位字段

First, fix your typos (there are several of them, as you can see in the comment section of your question). 首先,修正您的错别字(有很多错别字,您可以在问题的评论部分中看到)。 Second, don't try to create a zero width bit field in this context. 其次, 不要尝试在这种情况下创建零宽度的位字段。

There is much going on here, so I will try to address these items one at a time. 这里有很多事情要做,所以我将尝试一次解决这些问题。

Enum Value Scope Resolution - in C++, values of an enumeration are declared in the same scope as the enumeration (unlike C#, wherein the enumeration is a namespace for its values). 枚举值范围解析-在C ++中,枚举的值在与枚举相同的范围内声明(与C#不同,在C#中,枚举是其值的命名空间)。 The fully qualified scope of your enum values are ::MyClass::a and ::MyClass::b . 枚举值的完全限定范围是::MyClass::a::MyClass::b All prefixes components in the fully qualified scope that are shared with the referencing code's scope may be omitted as long as omission does not result in the desired value being hidden by another value of the same name. 与引用代码的作用域共享的完全限定作用域中的所有前缀组件都可以省略,只要省略不会导致所需值被相同名称的另一个值隐藏。 So in the example above, you may refer to the values as a or b . 因此,在上面的示例中,您可以将值称为ab

The other problem is that MyClass::myStruct::enumValue is being declared as a zero-length bit-field because MyClass::a has the numeric value of 0. 另一个问题是MyClass::myStruct::enumValue被声明为零长度位字段,因为MyClass::a的数值为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM