简体   繁体   中英

c++ function ptr in unorderer_map, compile time error

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!

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 &);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM