Solved

Can Enum's be declared forward?

Posted on 1998-03-19
6
418 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
Comment Utility
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
Comment Utility
Isn't K&R C, not C++?  
How do you encapsulate it in a typedef.?
0
 
LVL 5

Expert Comment

by:yonat
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Author Comment

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

Expert Comment

by:Nexial
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now