I am working on a VideoServer for my company, and in making the cgi files for the Web interface, I came across the following error:
" invalid conversion from ‘int’ to ‘VideoOutputMode‘ "
This was from line 4 of the below code, in the WebInterface file:

form_iterator vout_mode = formData.getElement("DisplaySettingsVOutMode");
				   if(!vout_mode->isEmpty() && vout_mode != (*formData).end())
				   {
				     display.setMode(atoi((**vout_mode).c_str()));
				   }
				 
				   display.saveConfiguration();
			   }

The header file contains the following definition inside the class DisplaySettings:

void setMode(VideoOutputMode mode) { mode_ = mode; };

The enumeration VideoOutputMode, also in the header file, is defined as follows:

enum VideoOutputMode
  {
    VO_MODE_LOCAL = 0,
    VO_MODE_REMOTE = 1,
    VO_MODE_PIP = 2,
    VO_MODE_FULLSCREEN = 5
  };

How do I get the function setmode to properly take the enumeration values? Thanks.

would you not do:

void setMode(VideoOutPutMode mode)
{
   mode_ = mode;
}

//And then

SetMode(VideoOutputMode::VO_MODE_LOCAL);      //I THINK thats how you call it.

enum -> int is a one-way street. You can not convert int -> enum directly. What you can do is redefine

void setMode(VideoOutPutMode mode)

as

void setMode(int mode)

HOWEVER... The former version (the one with the enum) provides direct control over the values that can be passed into that function. You do not have to worry about erroneous values and thus can drop much error checking. If you move to the int version you can now potentially get any integer value. I'd suggest if you make the change you implement some type of control logic for invalid values.

Casting from int to enum just requires an explicit cast (it is meant to reduce unintentional (implicit) casts). Just use a static_cast. As so:

display.setMode(static_cast<VideoOutputMode>(atoi((**vout_mode).c_str())));

Casting from int to enum just requires an explicit cast

Yes, that will allow for the call to happen, but there is still the question of valid data. The cast takes away the guarantee that the value will be correct. I guess that it depends on the situation which is more appropriate.

Yes, that will allow for the call to happen, but there is still the question of valid data. The cast takes away the guarantee that the value will be correct. I guess that it depends on the situation which is more appropriate.

Sure. That's another reason why casts are not desirable. The OP could also define a custom conversion function:

enum color { red = 0, green = 1, blue = 2 };

color to_color(const std::string& s, color default_value = color::red) {
  int i;
  std::stringstream(s) >> i;
  if((i >= 0) && (i <= 2))
    return static_cast<color>(i);
  else
    return default_value;
};

But that puts overhead. It's a matter of choice really, safety or performance. There is no way to convert an int whose value is determined at run-time, to an enum type without having to make that choice.

What you can do is redefine

void setMode(VideoOutPutMode mode)

as

void setMode(int mode)

I tried this already. When void setMode(VideoOutputMode mode) {mode_ = mode;} is used, then I have to change several other defenitions so I don't have to type-cast them. And mike_2000_17's suggestion ( display.setMode(static_cast<VideoOutputMode>(atoi((**vout_mode).c_str()))); gave me the exact same error as before.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.