【发布时间】:2013-01-19 21:46:14
【问题描述】:
嘿,所以我正在制作一个以字符串为键、成员函数指针为值的映射。我似乎无法弄清楚如何添加到地图,这似乎不起作用。
#include <iostream>
#include <map>
using namespace std;
typedef string(Test::*myFunc)(string);
typedef map<string, myFunc> MyMap;
class Test
{
private:
MyMap myMap;
public:
Test(void);
string TestFunc(string input);
};
#include "Test.h"
Test::Test(void)
{
myMap.insert("test", &TestFunc);
myMap["test"] = &TestFunc;
}
string Test::TestFunc(string input)
{
}
【问题讨论】:
-
猜测,但
&Test::TestFunc? -
似乎修复了参数中的一个错误,但我仍然收到插入错误
-
@Kosmo 那是因为
insert不能那样工作。 -
“这似乎不起作用”是什么意思?
-
具体一点,引用错误。关于 insert(),您必须将其转换为正确的类型,即 pair
,又名 map::value_type。
标签: c++ map member-function-pointers