简体   繁体   English

用C ++ wstring枚举

[英]C++ wstring to enum

I have defined an enum like this: 我已经定义了一个这样的枚举:

enum eFeature
{
    eF_NONE=0,
    eF_PORT_A=1,
    eF_PORT_B=2,
    eF_PORT_C=3,
};

I would now like to convert a wstring (which is either "0", "1", "2" or "3") to eFeature. 我现在想将wstring(“ 0”,“ 1”,“ 2”或“ 3”)转换为eFeature。

I tried 我试过了

eFeature iThis;
iThis = _wtoi(mystring.c_str());

But the compiler tells me "A value of type 'int' can not be assigned to an entity of the type eFeature." 但是编译器告诉我“不能将类型为'int'的值分配给类型为eFeature的实体。”

Can somebody help? 有人可以帮忙吗? Thank you. 谢谢。

You are attempting to assign an int to an enum , which is not allowed. 您试图将int分配给enum ,这是不允许的。 Leaving the wstring distraction aside, what you are doing is the equivalent of 撇开wstring注意力,您在做什么等同于

eFeature iThis;
iThis = 42;

You first need to cast the int to the enum type: 您首先需要将int转换为enum类型:

eFeature iThis;
iThis = static_cast<eFeature>(42);

Obviously, you would need to perform some kind of range checking first. 显然,您首先需要执行某种范围检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM