1
기본적으로 std :: map 유형의 정적 멤버에 항목을 추가하는 클래스에서 정적 함수를 호출하려고합니다.정적 함수를 사용하여 정적 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;
}
편집 : 나는이 코드를 컴파일 할 때, 그것은 나에게 오류 제공
:
derp.hpp:24:41: error: cannot declare member function ‘static bool Foo::addEntry(std::string)’ to have static linkage [-fpermissive]
어떻게해야합니까를 질문 D 물어 잊어 버렸습니까?
원래의 "문제"를
모든 클래스 멤버가 정적 인 경우 클래스를 제거하고 네임 스페이스를 사용하지 않는 것이 좋습니다. 일부 기능을 비공개로 유지하려는 경우 헤더 파일에 게시하지 않습니다. –
수정 된 답변보기 –
"그냥 헤더 파일에 게시하지 않습니까?" – StAlRuth