Solved

Can Enum's be declared forward?

Posted on 1998-03-19
6
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

749 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