小编qmw*_*brv的帖子

无法调用存储在unordered_map中的函数

我试图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)

c++ unordered-map

0
推荐指数
1
解决办法
41
查看次数

标签 统计

c++ ×1

unordered-map ×1