基本上,我想在我的类中调用一个静态函数,它向 std::map 类型的静态成员添加一个条目。
class Foo
{
private:
static std::map<std::string, int, StringCompare> mymap;
public:
static bool addEntry(std::string id);
};
std::map<std::string, int, StringCompare> Foo::mymap;
static bool Foo::addEntry(std::string id)
{
int a = 0;
return (mymap.insert ( std::pair<std::string, int> (id, a))).second;
}
编辑:忘了问问题 D:
当我编译这段代码时,它给我错误:
derp.hpp:24:41: error: cannot declare member function ‘static bool Foo::addEntry(std::string)’ to have static linkage [-fpermissive]
我该怎么办?
最佳答案
对于你原来的“问题”,使用这个:
Foo::addEntry("myId");
对于您忘记的问题,只需删除 static
关键字即可。
关于c++ - 如何使用静态函数填充静态 std::map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12722953/