Tyler Durden
Tyler Durden

Reputation: 11532

Refer to an enum inside a struct

I have an struct defined like this in a header file:

struct MessageA {
   enum Status {
      eReady, eNotReady 
   };
};

Later, when I try to use this enum:

#include <MessageA.hh>
...
if( status != MessageA.Status.eReady ) continue;

I get the error:

expected primary-expression before '.' token
'Status' has not been declared

I tried the following and it worked:

if( status != MessageA::eReady ) continue;

However, if use:

if( status != MessageA::Status::eReady ) continue;

Then I get the error:

   Status is not a class or a namespace

If I needed to specify the name of the enum fully qualified (such as if there were multiple enums with the same values inside) how should I do it?

Upvotes: 0

Views: 2662

Answers (2)

user1593881
user1593881

Reputation:

Use the scope operator:

MessageA::Status::eReady;

Also note that prior to C++11 labels of enums were not scoped in which case you would be using the following code:

MessageA::eReady;

Upvotes: 4

Put ; in end of the struct :

struct MessageA {
   enum Status {
      eReady, eNotReady 
   };
};

Then use of enum elements like bellow :

int main()
{
    if (MessageA::Status::eNotReady == 0)
        printf("ok");

    return 0;
}

Upvotes: 1

Related Questions