如何解决地图查找错误:无法将对转换为枚举值
我看到以下代码建议用于在堆栈溢出时在另一个线程中创建地图查找(我已出于我的目的对其进行了修改,但一般结构保持不变)。
#include <string>
#include <map>
#include <algorithm>
using namespace std;
namespace kwd { //define namespace
enum Options{ //enumerate alternatives
Option_Invalid,assert
//repeat for more options
};
Options resolveOptions(string input){ //find enum values
static const map < string,Options> optionStrings { //inputmap
{ "assert",assert }
//repeat for more options
};
//finally closing with
auto itr = optionStrings.find(input);
if( itr != optionStrings.end() ) {
return *itr;
}
return Option_Invalid;
}
}
string commentMake(string commentarray[]) {
int commentarraylen =sizeof(commentarray)/sizeof(commentarray[0]);
string comment = "";
for(int i =0; i < commentarraylen; i++){
switch (kwd::resolveOptions(commentarray[i])){
case kwd::Option_Invalid:
comment += commentarray[i] + " ";
break;
//repeat for more options
}}}
我需要它或其他方法来快速查看大量不同的案例,所以我想避免使用 If 语句(因为我知道它们很慢)。但是,当我运行它时,我收到以下错误:
spoofcode.cpp: In function ‘kwd::Options kwd::resolveOptions(std::string)’:
spoofcode.cpp:28:16: error: cannot convert ‘const std::pair<const std::__cxx11::basic_string<char>,kwd::Options>’ to ‘kwd::Options’ in return
28 | return *itr;
| ^~~~
| |
| const std::pair<const std::__cxx11::basic_string<char>,kwd::Options>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。