tags:

views:

142

answers:

1

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.

+6  A: 

You should take the enum by value, rather than by const reference. It's small enough to fit into an int, so there is no performance penalty or anything like it.

But, from what you're describing, it sounds like somebody has #defined INCORRECT_FRAME to 0 elsewhere. You should put something like the following in the line above it:

#ifdef INCORRECT_FRAME
#error Whoops, INCORRECT_FRAME already defined!
#endif

BTW, the #ifndef thingy (for your header files) is called an include guard. :-)

Chris Jester-Young
+1, but I believe that accepting by const reference should still work. But I wouldn't be surprised if that fixes it. Who knows how old GCC is on RHEL4?
rlbond
@rlbond: I think const reference should work too...see the OP's comment about how it sometimes works and sometimes not. That's what led me to believe some other code `#define`'d a new value of `INCORRECT_FRAME` later.
Chris Jester-Young
@ribond, gcc version on RHEL 4 is 3.4.3 :)
rocknroll