GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Dec-2004, 22:23
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,539
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Can enum have same name as class?


After spending several weeks modifying source code from my textbook, I ran into a problem I didn't anticipate. I had given my enum's the same name as the classes they are in. Naturally, I get nasty errors when I compile.

Is there a way to keep all these names the same? (I thought C++ would be more forgiving, but I guess not). If I have to change the names, is there an easy way to do this? (I got lost when I started to do it by hand and ended up w/ more errors).

I'm attaching the source so you can understand just how bad I screwed up; I'm aware there are other errors present but I will attack them later. Unless I find out they are all related, then I might give up; I don't want to recode this mess again. (Can you tell I'm getting burnt out after 8 months in one class? )
Attached Files
File Type: zip src.zip (6.8 KB, 50 views)
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #2  
Old 08-Dec-2004, 08:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by crystalattice
After spending several weeks modifying source code from my textbook, I ran into a problem I didn't anticipate. I had given my enum's the same name as the classes they are in. Naturally, I get nasty errors when I compile.

Is there a way to keep all these names the same? (I thought C++ would be more forgiving, but I guess not). If I have to change the names, is there an easy way to do this? (I got lost when I started to do it by hand and ended up w/ more errors).

I'm attaching the source so you can understand just how bad I screwed up; I'm aware there are other errors present but I will attack them later. Unless I find out they are all related, then I might give up; I don't want to recode this mess again. (Can you tell I'm getting burnt out after 8 months in one class? :cry: )


I'll not try to debug your dilemma. Here's a suggestion: Try to compile each program one at a time and resolve all errors (and at least reconcile yourself to any remaining warnings).

For example (if you are using GNU compiler):

g++ -c auto_name.cpp

This just takes this one file and tells you some things.

I got the following, rather helpful hints:

Quote:
In file included from auto_name.cpp:7:
auto_name.h:74: error: prototype for `AutoName::AutoName(AutoName)' does not
match any in class `AutoName'
auto_name.h:23: error: candidates are: AutoName::AutoName(const AutoName&)
auto_name.h:26: error: AutoName::AutoName(Name)
auto_name.h:66: error: AutoName::AutoName()
auto_name.h:74: error: invalid constructor; you probably meant `AutoName (const
AutoName&)'
auto_name.h:74: error: syntax error before `{' token
auto_name.h:76: error: ISO C++ forbids declaration of `myAutoName' with no type
auto_name.h:76: error: `initialAutoName' was not declared in this scope
auto_name.h:77: error: parse error before `}' token
auto_name.cpp: In member function `void AutoName::read(std::istream&)':
auto_name.cpp:24: error: `myAutoNameName' undeclared (first use this function)
auto_name.cpp:24: error: (Each undeclared identifier is reported only once for
each function it appears in.)
auto_name.cpp:62: error: `SANTA_FE' undeclared (first use this function)
auto_name.cpp: In member function `void AutoName::print(std::ostream&) const':
auto_name.cpp:80: error: `EXPEDITION' undeclared (first use this function)
auto_name.cpp:82: error: `EXCURSION' undeclared (first use this function)
auto_name.cpp:86: error: `VIPER' undeclared (first use this function)
auto_name.cpp: At global scope:
auto_name.cpp:96: error: syntax error before `::' token
auto_name.cpp:120: error: syntax error before `::' token

If you are using some other compiler, try it on each file with the -c option.

For example Microsoft VisualC++ from Visual Studio 6 try

cl -c auto_name.cpp /GX

Quote:
auto_name.h(74) : error C2511: 'AutoName::AutoName' : overloaded member function 'void (class AutoName)' not found in '
utoName'
auto_name.h(23) : see declaration of 'AutoName'
auto_name.cpp(24) : error C2065: 'myAutoNameName' : undeclared identifier
auto_name.cpp(62) : error C2065: 'SANTA_FE' : undeclared identifier
auto_name.cpp(80) : error C2065: 'EXPEDITION' : undeclared identifier
auto_name.cpp(80) : error C2051: case expression not constant
auto_name.cpp(82) : error C2065: 'EXCURSION' : undeclared identifier
auto_name.cpp(82) : error C2051: case expression not constant
auto_name.cpp(86) : error C2065: 'VIPER' : undeclared identifier
auto_name.cpp(86) : error C2051: case expression not constant
auto_name.cpp(96) : error C2143: syntax error : missing ';' before 'tag::id'
auto_name.cpp(96) : error C2501: 'Auto_Name' : missing storage-class or type specifiers
auto_name.cpp(96) : fatal error C1004: unexpected end of file found

With Microsoft Visual C++ from the Visual C++ 2005 Express beta download try

cl -c auto_name.cpp /EHsc

Quote:
MYPATH\auto_name.h(74) : error C2511: 'AutoName::AutoName(AutoName)' : overloaded member functi
on not found in 'AutoName'
MYPATH\auto_name.h(23) : see declaration of 'AutoName'
auto_name.cpp(24) : error C2065: 'myAutoNameName' : undeclared identifier
auto_name.cpp(62) : error C2065: 'SANTA_FE' : undeclared identifier
auto_name.cpp(74) : error C2050: switch expression not integral
auto_name.cpp(80) : error C2065: 'EXPEDITION' : undeclared identifier
auto_name.cpp(80) : error C2051: case expression not constant
auto_name.cpp(82) : error C2065: 'EXCURSION' : undeclared identifier
auto_name.cpp(82) : error C2051: case expression not constant
auto_name.cpp(86) : error C2065: 'VIPER' : undeclared identifier
auto_name.cpp(86) : error C2051: case expression not constant
auto_name.cpp(96) : error C2143: syntax error : missing ';' before 'AutoName::operator`++''
auto_name.cpp(96) : error C2501: 'Auto_Name' : missing storage-class or type specifiers
auto_name.cpp(97) : error C2556: 'int AutoName::operator ++(void)' : overloaded function differs only by return type fro
m 'AutoName AutoName::operator ++(void)'
MYPATH\auto_name.h(33) : see declaration of 'AutoName::operator`++''
auto_name.cpp(97) : error C2371: 'AutoName::operator`++'' : redefinition; different basic types
MYPATH\auto_name.h(33) : see declaration of 'AutoName::operator`++''
auto_name.cpp(98) : error C2050: switch expression not integral
auto_name.cpp(104) : error C2051: case expression not constant
auto_name.cpp(106) : error C2051: case expression not constant
auto_name.cpp(110) : error C2051: case expression not constant
auto_name.cpp(114) : error C2065: 'COMPANY_OVERLOW' : undeclared identifier
auto_name.cpp(120) : error C2143: syntax error : missing ';' before 'AutoName::operator`++''
auto_name.cpp(120) : error C2501: 'Auto_Name' : missing storage-class or type specifiers
auto_name.cpp(120) : error C2086: 'int Auto_Name' : redefinition
auto_name.cpp(96) : see declaration of 'Auto_Name'
auto_name.cpp(121) : error C2556: 'int AutoName::operator ++(int)' : overloaded function differs only by return type fro
m 'AutoName AutoName::operator ++(int)'
MYPATH\auto_name.h(34) : see declaration of 'AutoName::operator`++''
auto_name.cpp(121) : error C2371: 'AutoName::operator`++'' : redefinition; different basic types
MYPATH\auto_name.h(34) : see declaration of 'AutoName::operator`++''
auto_name.cpp(122) : error C2065: 'AutoNameName' : undeclared identifier
auto_name.cpp(122) : error C2146: syntax error : missing ';' before identifier 'savedAutoNameName'
auto_name.cpp(122) : error C2065: 'savedAutoNameName' : undeclared identifier
auto_name.cpp(124) : error C2050: switch expression not integral
auto_name.cpp(130) : error C2051: case expression not constant
auto_name.cpp(132) : error C2051: case expression not constant
auto_name.cpp(136) : error C2051: case expression not constant

Maybe you can see some light at the end of the tunnel.

Another suggestion (which may or may not be applicable to this case, but is something that I generally do):

When creating new code or modifying code, try compiling it after a few lines or a few tens of lines. I don't wait until I have 10,000 lines spread over 34 files and then try to compile it all together.

For new programs, a top-down approach says: first create all classes, structs, etc. and see if they are correct (or create them as you recognize their need during the development). Then start at the top (usually main()) and try it. For functions, put in the prototypes, and try to compile (use the -c compiler switch so that you don't actually have to have the function bodies yet.) That's one way; it's certainly not the only way, and I don't claim it's the best way for everybody.

It works for me. Your Mileage May Vary.

Regards,

Dave

"We can face our problem. We can arrange such facts as we have
with order and method."

--- Hercule Poirot
in Murder on the Orient Express
  #3  
Old 08-Dec-2004, 11:37
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
The best way to get around this is to create the enums in a namespace.
Example:

CPP / C++ / C Code:
namespace enumerated
{
  enum RPS { eRock, ePaper, eScissors };
}

class RPS
{
  enumerated::RPS choice; // eRock, ePaper, or eScissors
};
__________________
-Aaron
  #4  
Old 08-Dec-2004, 16:43
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,539
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice
Thanks for the tidbits.

I may not have explained properly, but the source code was taken from the textbooks web site and modified to do what the question asked for. That's probably why I have so many errors; apart from rewriting small bits within each class (read: make new errors ), I didn't get a chance to debug it while coding. I had to add an extra class due to requirements, and me and classes don't get along too well ;-).

I got beaucoup errors, just like in dave's post, so I know there's quite a bit wrong. And please don't think I was asking for someone to debug it; I just didn't know if some of my errors were due to having the same names.

Hope that explains this mess.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
 
 

Recent GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Introduction to .NET LuciWiz .NET Forum 5 09-Aug-2007 04:53
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 C++ Forum 2 19-Dec-2004 06:06
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 03:38
hashing help saiz66 C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.