Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can Enum's be declared forward?

Posted on 1998-03-19
6
Medium Priority
?
438 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 150 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

963 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