Link to home
Start Free TrialLog in
Avatar of nietod
nietod

asked on

Can Enum's be declared forward?

I am trying to switch from Microsoft Vissual C++ to Borland C++ Builder 3.  In VC I have dozens of cases of foreward declared enum's (created by a utuility) that look like this

enum SomeEnum;

    *      *      *
enum SomeEnum
{
    EnumItem1,
   EnumItem2
};

This worked fine in VC, but is causing a problem for BC.  It complains that the

'SomeEnum' must be a previously defined enumeration tag.

So VC thinks it is legal and BC seems to think it is not.  Who is right?  Is there a way to get this past BC?  (Other than moving the enum declaration).
ASKER CERTIFIED SOLUTION
Avatar of Nexial
Nexial

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

ASKER

Isn't K&R C, not C++?  
How do you encapsulate it in a typedef.?
I don't know what Nexial means by "incomplete typedef", but the new C++ standard uses the new keyword "typename" for that purpose:

    typename SomeEnum;

I don't know if BC supports this, though.
Avatar of nietod

ASKER

Thanks.  That is vaguely familiar.  I'll look into that tomorrow.
Encapsulate an enum in a typedef:

typedef enum enum_name {name1, name,...};

It has exactly the same syntax as a typedef on a struct or
union except for incomplete enumeration types.   The tag  (enum_name) without a following list must refer to an in-scope
specifier with a list.   So the enum list must be defined within the same scope, but may follow the typedef declaration.   I have used this in ANSI standard C, so I know it works (if the compiler didn't lie).

I think the same holds true for C++, but I am not absolutely sure.

Obviously, fail my answer if it doesn't work for you.


Avatar of nietod

ASKER

Things have gotten weird.  Builder 3.0 definitily would not handled forward enum's like I showed above.  Both in my real code and in a small example.  I went to test both of your suggestions and now it works fine.  That is, without employing the suggestions.  i am confussed.  I'll fool with it some more.  I don't wish to accept an answer that I haven't tested.  But if I can't test it, I'll accept Nexial's answer assuming it is right.