c++ function ptr in unorderer_map, compile time error

风流意气都作罢 提交于 2019-12-12 01:23:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!