1

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. 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

Thanks, Adam

amitchone
  • 1,630
  • 3
  • 21
  • 45
  • 1
    First off, in defining enum, you should use comma instead of semicolon. Secondly, your `myEnum::a` is 0, which leads to start to have zero width. Lastly, you are missing a semicolon after MyClass definition. – YiFei Sep 17 '18 at 15:21
  • 1
    1) If you get a compilation error, why didn't you include it? Is it a secret? 2) The errors that I got, [when running your example](https://ideone.com/KgUCC5) has nothing to do with being unable to reference the `enum` elements. Why do you think, that this is the case? – Algirdas Preidžius Sep 17 '18 at 15:22
  • @YiFei You are correct of course regarding the semicolons - this is just a typo between my code and the question. You mean I need to define a type of `myEnum::a`? – amitchone Sep 17 '18 at 15:23
  • I suggest posting the actual code, or posting at least the errors you get. You don't need to define a type, but the width of bitfield `enumValue` will be 0, which is not allowed by C++ standard. – YiFei Sep 17 '18 at 15:24
  • @AlgirdasPreidžius Error message provided and syntax errors due to me writing out the question are fixed. – amitchone Sep 17 '18 at 15:24
  • A bitfield member of length `0` doesn't make any sense, no? – πάντα ῥεῖ Sep 17 '18 at 15:25
  • @AdamMitchell 1) "_You mean I need to define a type of `myEnum::a`?_" That's not what was said. The value of `a` is `0`. You can't have a `struct` variable be `0` bits long. 2) "_Error message provided and syntax errors due to me writing out the question are fixed._" [The only remaining error](https://ideone.com/jbhigp), is, still, **not** related to not being able reference the `myEnum::a`. Why do you think this is the case? – Algirdas Preidžius Sep 17 '18 at 15:25

3 Answers3

2

Enums don't have a scope. Therefore applying the scope resolution operator :: to myEnum is wrong.

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.

Outside of MyClass, it can be resolved with MyClass::a.


P.S. The value of a is 0. A zero width bitfield may not be named. There is some sort of disconnect between what you're trying to do, and what you're trying to achieve by doing it.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Type safe enums (`enum class`) *do* have a scope though. Maybe add `enum class` to your answer. – Jesper Juhl Sep 17 '18 at 16:35
  • @JesperJuhl I would consider that outside the *scope* of my answer. No need to confuse OP with a new concept at this point. I don't think a scoped enum is a good choice for him, since he intends to use the numeric value directly. – eerorika Sep 17 '18 at 16:39
0

myEnum::a is defined as 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.

Fureeish
  • 12,533
  • 4
  • 32
  • 62
0

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). The fully qualified scope of your enum values are ::MyClass::a and ::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.

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.