c++ - 没有匹配的成员函数调用 "insert"std::unordered_map

标签 c++ c++11 typedef unordered-map

我正在尝试将 string 散列为 指向接受字符串的 void 函数的指针。尝试将我的键值对插入 map 时出现以下错误:

“没有匹配的成员函数来调用”insert”

我不确定如何解释这个错误。

我想我要么为插入传递了错误的类型,要么函数引用不正确,要么函数指针的类型定义错误。

#include <string>
#include <unordered_map>
using namespace std;

void some_function(string arg)
{
  //some code
}

int main(int argc, const char * argv[]) {


    typedef void (*SwitchFunction)(string);
    unordered_map<string, SwitchFunction> switch_map;

    //trouble with this line
    switch_map.insert("example arg", &some_function); 
}   

如有任何建议,我们将不胜感激。

最佳答案

如果您查看 std::unordered_map::insert 的重载,你会看到这些:

std::pair<iterator,bool> insert( const value_type& value );
template< class P >
std::pair<iterator,bool> insert( P&& value );
std::pair<iterator,bool> insert( value_type&& value );
iterator insert( const_iterator hint, const value_type& value );
template< class P >
iterator insert( const_iterator hint, P&& value );
iterator insert( const_iterator hint, value_type&& value );
template< class InputIt >
void insert( InputIt first, InputIt last );
void insert( std::initializer_list<value_type> ilist );

没有 insert(key_type, mapped_type),这是您要尝试执行的操作。你的意思是:

switch_map.insert(std::make_pair("example arg", &some_function)); 

关于c++ - 没有匹配的成员函数调用 "insert"std::unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33613983/

相关文章:

c++ - 如何测试类型是否是具有非类型参数的模板的特化?

c++ - nullptr 可以用作变量参数(varargs)吗?

部分模板的 C++ typedef

c++ - 奇怪的 C++ namespace 解析怪癖和 g++ 与 clang++

c++ - 在编译时将 std::array 的每个元素相乘

c++ - 模板类的typedef

ios - 单例还是实用类?

c++ - 尝试使用 Bonjour 运行演示应用程序,但无法发现在同一主机上运行的服务器

c++ - 为什么 "-1"在signed int 中是 11111111 而不是 10000001

c++ - 自定义系统托盘通知 Qt