问题
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:
error: no matching function for call to 'std::unordered_map, void (*)(std::basic_string&)>::insert(std::pair, void (Test::Example::*)(std::basic_string&)>&)'
Thx!
回答1:
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 &);
来源:https://stackoverflow.com/questions/30533797/c-function-ptr-in-unorderer-map-compile-time-error