Solved

Can Enum's be declared forward?

Posted on 1998-03-19
6
419 Views
Last Modified: 2011-10-03
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).
0
Comment
Question by:nietod
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
Nexial earned 50 total points
ID: 1183648
Per K&R (both 1 and 2) it is not legal.   See page 215 in K&R 2
-- "incomplete enumeration types do not exist"

However, if you encapsulate the enum in a typedef, then, since
incomplete typedefs do exist, you can get the same effect.
 
0
 
LVL 22

Author Comment

by:nietod
ID: 1183649
Isn't K&R C, not C++?  
How do you encapsulate it in a typedef.?
0
 
LVL 5

Expert Comment

by:yonat
ID: 1183650
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.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 22

Author Comment

by:nietod
ID: 1183651
Thanks.  That is vaguely familiar.  I'll look into that tomorrow.
0
 
LVL 1

Expert Comment

by:Nexial
ID: 1183652
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.


0
 
LVL 22

Author Comment

by:nietod
ID: 1183653
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.  
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question