0
我试图创建一个由int
和指向成员函数的指针组成的地图。创建成员函数指针的地图
class Factory
{
public:
typedef nts::IComponent *(*createFunction)(const std::string &value);
Factory();
~Factory();
nts::IComponent *createComponent(const std::string &type, const std::string &value);
private:
nts::IComponent *create4001(const std::string &value) const;
nts::IComponent *create4013(const std::string &value) const;
nts::IComponent *create4040(const std::string &value) const;
nts::IComponent *create4081(const std::string &value) const;
std::map<int, createFunction> map = {{4001, Factory::create4001},
{4013, Factory::create4013},
{4040, Factory::create4040}};
};
但是我有这个以下错误:
includes/Factory.hpp:24:68: error: could not convert ‘{{4001, ((Factory*)this)->Factory::create4001}, {4013, ((Factory*)this)->Factory::create4013}, {4040, ((Factory*)this)->Factory::create4040}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<int, nts::IComponent* (*)(const std::__cxx11::basic_string<char>&)>’
{4040, Factory::create4040}};
有什么想法?
阅读起来在使用它之前,您最喜欢的书中的主题。 'createFunction'不是成员函数指针的别名。它也缺少一个'const'限定符。 – LogicStuff
尝试'使用createFunction = nts :: IComponent *(const std :: string&);',然后使用'std :: map map;'。 –
@KerrekSB相同的错误 –