I am trying to std::map, with an enum class and a std::string but I am getting some error. I am using gcc 4.4.7 with -std=c++0x (this is fixed)
At .h file:
enum class state_t{
unknown,
off,
on,
fault
};
typedef std::map<state_t,std::string> statemap_t;
At .cpp file:
statemap_t state={
{state_t::unknown,"unknown"}
{state_t::off,"off"}
{state_t::on,"on"}
{state_t::fault,"fault"}
}
The method to allow a state transitionis like:
Foo::allowStateChange(const state_t localState, const state_t globalState, const state_t newState){
//Some code to verify if the state transition is allowed.
std::cout << "Device Local State:" << state.find(localState)->second << "Device Global State:" << state.find(globalState)->second << "Device New State:" << state.find(newState)->second << std::endl;
}
When compilling, I get next error: error: invalid operands of types 'state_t' and 'state_t' to binary 'operator<'
If I change the enum class state_t
to enum state_t
it works. Is there any way to find in the map with an enum class?
Thanks in advance.
The following code works just fine (on Visual Studio 2015 (v140); which compiler is used in your case?):
#include <string>
#include <iostream>
#include <map>
using namespace std;
enum class state_t {
unknown,
off,
on,
fault
};
typedef std::map<state_t, std::string> statemap_t;
statemap_t state = {
{ state_t::unknown,"unknown" },
{ state_t::off,"off"},
{ state_t::on,"on"},
{ state_t::fault,"fault"}
};
void allowStateChange(const state_t localState, const state_t globalState, const state_t newState) {
//Some code to verify if the state transition is allowed.
std::cout
<< "Device Local State:"
<< state.find(localState)->second
<< ", Device Global State:"
<< state.find(globalState)->second
<< ", Device New State:"
<< state.find(newState)->second
<< std::endl;
}
int main()
{
allowStateChange(state_t::on, state_t::off, state_t::fault);
return 0;
}
BWT, there is a misspell "unkmown" in the state_t.
I assume the GCC compiler version you use does not support all of the infrastructure associated with enum classes. You'll therefore need to implement the missing operators yourself, as shown below:
inline bool operator <(const state_t left, const state_t right)
{
return static_cast<int>(left) < static_cast<int>(right);
}
inline bool operator >(const state_t left, const state_t right)
{
return static_cast<int>(left) > static_cast<int>(right);
}
In C++11, these functions are likely implemented via template specialization, using std::underlying_type for the static_cast and qualifiers tying them specifically to enum classes, some of which are probably not available under -std=c++0x for your specific compiler version
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.