enum problem

Started by
7 comments, last by CraZeE 18 years, 10 months ago
i have an enumeration problem in C++ could someone tell me whats up with this code? the compiler tells me several things, that theres a "parse error" before the 'numeric constant' in the enum dec on line 5 also, when i declare ERRCODE result, it says its storage size is unknown. and lots more...but i think it all leads back to the first problem #include <iostream> using namespace std; enum ERRCODE { SUCCESS, ERROR }; ERRCODE Factor(int , int&, int&); int main() { int number, squared, cubed; ERRCODE result; cout<<"enter a number (0-20): "; cin>>number; result=Factor(number, squared, cubed); if(result==SUCCESS) { cout<<"\nnumber: "<<number<<endl; cout<<"\nsquared: "<<squared<<endl; cout<<"\ncubed: "<<cubed<<endl; } else cout<<"ERROR FOUND!!"<<endl; char res; cin>>res; return 0; } ERRCODE Factor(int n, int &rSquared, int &rCubed) { if(n>20) return ERROR; else { rSquared=n*n; rCubed=n*n*n; return SUCCESS; } }
Advertisement
glancing once at the code....you need to start your enum...you cant just declare an enum without at least initializing the first one...at least im pretty sure...


enum ERRCODE { SUCCESS = 0, ERROR };

try that...if it still breaks...its most likely something else
We have youth, how about a fountain of smart.e4 e5 f4 d5
I just ran it on MSVC, and it works fine. I don't see any compiling problems...
Usefull Links:__________________________________________________________________|GameDev.net FAQ|2d Game Tutorials|Nehe's OpenGL Tutorials||Cpp-Home.com|
Quote:Original post by Tang of the Mountain
enum ERRCODE { SUCCESS = 0, ERROR };


You only need to set the first one if you want it to start at somthing besides 0.
i initialized it to zero(SUCCESS in the enum) but it still did not work
and i use Dev 4.9.8.0, for the record
oh, and sorry about the long code, i just found out about the

tags; i'll use them from now on
i appreciate any help given
If it works on MSVC and not Dev-cpp, the only guess I have is that "SUCCESS" or "ERROR" is already defined as somthing. This is just a guess, but try changig the names to SUCCESS1 and ERROR1 or somthing...
wow, that actually worked
but where in iostream would ERROR and SUCCESS be defined?
i can see ERROR being in there, but not the other...
Err, there is another forum for programming spcefic questions, but whatever.
as a general rule of thumb, try to avoid really generic names. I know they're convenient, but it also means they have been convenient to thousands of other developers out there. I'm probly shooting myself saying this, but try to make the constant names a *bit* cryptic (yet readable) like appending an abbreviation or something. It makes names clashes all the less likely.

also, utilize namespaces when using enumerators and C++ style constants. #define constants tend to cause name pollutions and i dun think they can be contained within namespaces, but correct me if i'm wrong. Namespaced constants/enums are a better way to use 'common names' like success or error as long as they do not collide with preprocessor constants.
- To learn, we share... Give some to take some -

This topic is closed to new replies.

Advertisement