c ++函数ptr在unorderer_map中,编译时出错 [英] c++ function ptr in unorderer_map, compile time error
问题描述
我想实现一个unorderer_map实现为mapped_type,我正在看一些实现这些,但我不能使它的工作的例子。
这里是代码:
i am trying to implements an unorderer_map that implements as mapped_type, i was watching some examples that implements these but i cannot make it work. here is the code:
#include<string>
#include <unordered_map>
namespace Test
{
class Example
{
public:
Example()
{
auto aPair=std::make_pair("one",&Example::procesString);
map.insert(aPair);
}
void procesString(std::string & aString)
{
}
void processStringTwo(std::string & aString)
{
}
typedef void(*fnPtr)(std::string &);
std::unordered_map<std::string,fnPtr> map;
};
}
int main()
{
return 0;
}
我得到这个编译时错误:
I get this compile time error:
错误:没有匹配的函数调用'std :: unordered_map,void(*)(std :: basic_string&)> :: insert(std :: pair,void Test :: Example :: *)(std :: basic_string&)>&)'
error: no matching function for call to 'std::unordered_map, void (*)(std::basic_string&)>::insert(std::pair, void (Test::Example::*)(std::basic_string&)>&)'
Thx!
推荐答案
函数指针和成员函数指针是两个不同的东西。您需要向地图中添加一个自由函数,或者需要更改地图以使用成员函数指针。如果你想要一个成员函数指针,你可以使用:
A function pointer and a member function pointer are two different things. You either need to add a free function to the map or you need to change the map to take a member function pointer instead. If you want to take a member function pointer than you can use this:
typedef void(Example::*fnPtr)(std::string &);
这篇关于c ++函数ptr在unorderer_map中,编译时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!