1

I'm creating a new enum:

enum WState {SLEEPING=2, WAITING_FOR_DATA=3, SENDING=4, IDLE=5, ERROR=6};

Then I'm trying to initialize a variable of that enum type to a default state straight after.

WState CurrentState = WState::ERROR;

I can't figure out the correct syntax or maybe I'm missing some vital keywords when searching for an answer. It says:

data member initializer not allowed

enter image description here

4

1 回答 1

1

In C++11, what you are doing is allowed. In C++03, however, you have to perform the initialization in the class constructor (possibly in an initialization list, as shown below):

class Wireless
{
public:
    enum WState { /* ... */, ERROR = 6 };
    WState CurrentState;
    Wireless() : CurrentState(WState::ERROR)
    { /* ... */ }

    // ...
};
于 2013-03-30T11:54:38.643 回答