Passing enums to functions in C++
- by rocknroll
Hi all,
I have a header file with all the enums listed (#ifndef #define #endif construct has been used to avoid multiple inclusion of the file) that I use in multiple cpp files in my application.One of the enums in the files is
enum StatusSubsystem {ENABLED,INCORRECT_FRAME,INVALID_DATA,DISABLED};
There are functions in the application delcared as
ShowStatus(const StatusSubsystem&);
Earlier in the application when I made calls to the above function like
ShowStatus(INCORRECT_FRAME);
my application used to compile perfectly. But after some code was added The compilation halts giving the following error:
File.cpp:71: error: invalid conversion from `int' to `StatusSubsystem'
File.cpp:71: error: initializing argument 1 of `void Class::ShowStatus(const StatusSubsystem&)
I checked the code for any conflicting enums in the new code and it looked fine.
My Question is what is wrong with the function call that compiler shows as erroneous?
For your reference the function definition is:
void Class::ShowStatus(const StatusSubsystem& eStatus)
{
QPalette palette;
mStatus=eStatus;//store current Communication status of system
if(eStatus==DISABLED)
{
//select red color for label, if it is to be shown disabled
palette.setColor(QPalette::Window,QColor(Qt::red));
mLabel->setText("SYSTEM");
}
else if(eStatus==ENABLED)
{
//select green color for label,if it is to be shown enabled
palette.setColor(QPalette::Window,QColor(Qt::green));
mLabel->setText("SYSTEM");
}
else if(eStatus==INCORRECT_FRAME)
{
//select yellow color for label,to show that it is sending incorrect frames
palette.setColor(QPalette::Window,QColor(Qt::yellow));
mLabel->setText("SYSTEM(I)");
}
//Set the color on the Label
mLabel->setPalette(palette);
}
A strange side effect of this situation is it compiles when I cast all the calls to ShowStatus() as
ShowStatus((StatusSubsystem)INCORRECT_FRAME);
Though this removes any compilation error, but a strange thing happens. Though I make call to INCORRECT_FRAME above but in function definition it matches with ENABLED. How on earth is that possible? Its like while passing INCORRECT_FRAME by reference, it magically converts to ENABLED, which should be impossible. This is driving me nuts.
Can you find any flaw in what I am doing? or is it something else?
The application is made using C++,Qt-4.2.1 on RHEL4.
Thanks.