user2007843 user2007843 - 3 months ago 23
C++ Question

enum and struct call

I have a header file called Status.h shown in part below:

enum State {
OFFLINE = 0,
ONLINE = 1,
...
};

struct AgentStatus {
State statusInfo;
...
};


In my .cpp file I want to initialize this struct, set the status, and make a call to find the state every 10 seconds. This is my attempt:

AgentStatus s1;
s1.statusInfo = 0;
while(1){
std::cout<<"scanner status = "<<s1.statusInfo<<std::endl;
usleep(1000000);
}


the error I am getting is this:

error: invalid conversion from ‘int’ to ‘State’ [fpermissive]
s1.statusInfo = 0;


How can I set my struct to one of those values in the state enum?

Answer

You can implicitly convert from a State to a number, but not from a number to a State.

Try either

s1.statusInfo = OFFLINE;

or

s1.statusInfo = static_cast<State>(0);
Comments