gpt4 book ai didi

C++ wstring 枚举

转载 作者:行者123 更新时间:2023-11-27 23:15:13 26 4
gpt4 key购买 nike

我定义了这样一个枚举:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com