0

I found some problem with definition of enum inside a struct, I want to have something like:

typedef struct
{
    typedef enum { E1, E2, E3 } E;
    E e;
} S;

in VS2012 I have errors:

error C2071: 'E' : illegal storage class
error C2061: syntax error : identifier 'E'
error C2059: syntax error : '}'

I found an explanation of C2071 but it is not the same case: http://msdn.microsoft.com/en-us/library/deb3kh5w.aspx

gcc-4.9 says:

error: expected specifier-qualifier-list before ‘typedef’

the interesting thing is that the code:

typedef enum { E1, E2, E3 } E;
E e;

works fine in global scope and in function's body.

I've also tried to do it without typedef, but unfortunately there are still lot's of errors:

error C2011: 'E' : 'enum' type redefinition
see declaration of 'E'
error C2208: 'E' : no members defined using this type

I found similar reason: http://msdn.microsoft.com/en-us/library/ms927163.aspx but I do define members of the type.

1
  • Any use of typedef in a structure declaration is a syntax error. What do you expect that code to do? If you want the typedef visible only inside the structure declaration (which isn't really useful anyway): Structure declarations don't introduce a new scope (and if they did, the enumeration constants would then be unreachable from outside the structure). Commented Nov 26, 2014 at 21:02

1 Answer 1

3

You should declare your enum member like so:

typedef struct
{
    enum { E1, E2, E3 } e;
} S;

then you can do:

int main(void)
{
    S s;
    s.e = E1;

    /*  And so on  */
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.