我正在尝试将 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/