Sign in to follow this  
rayce

enum problem

Recommended Posts

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; } }

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this