简体   繁体   English

C ++函数ptr在unorderer_map中,编译时错误

[英]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. 我正在尝试实现undefined_map,该unorderer_map实现为apped_type,我正在观看一些实现这些效果的示例,但我无法使其工作。 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&)>&)' 错误:没有匹配函数调用'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 &);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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