我定义了这样一个枚举:
enum eFeature
{
eF_NONE=0,
eF_PORT_A=1,
eF_PORT_B=2,
eF_PORT_C=3,
};
我现在想将 wstring(可以是“0”、“1”、“2”或“3”)转换为 eFeature。
我试过了
eFeature iThis;
iThis = _wtoi(mystring.c_str());
但是编译器告诉我“不能将‘int’类型的值分配给 eFeature 类型的实体。”
有人可以帮忙吗? 谢谢。
最佳答案
您正试图将 int
分配给 enum
,这是不允许的。撇开 wstring
的干扰不谈,您所做的相当于
eFeature iThis;
iThis = 42;
您首先需要将int
转换为enum
类型:
eFeature iThis;
iThis = static_cast<eFeature>(42);
显然,您需要先执行某种范围检查。
关于C++ wstring 枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16760702/