我试图call_function使用一个字符串参数,并调用与该映射中的字符串相对应的函数,但出现错误。我该如何解决?为什么不起作用?
#include <iostream>
#include <unordered_map>
using namespace std;
class xyz {
public:
unordered_map< std::string, void(xyz::*)()> arr{
{ "user", &xyz::func_user},
{ "pwd", &xyz::func_pwd},
{ "dir", &xyz::func_dir}
};
void call_function(std::string x) {
arr.at( x)();// Error: term does not evaluate a function taking 0 arguments
}
void func_user(){
cout << "func_user" << endl;
}
void func_pwd(){
cout << "func_pwd" << endl;
}
void func_dir(){
cout << "func_dir" << endl;
}
};
int main(){
xyz a;
a.call_function( "dir");
}
Run Code Online (Sandbox Code Playgroud)