2012-10-04 4 views
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 물어 잊어 버렸습니까?

원래의 "문제"를
+0

모든 클래스 멤버가 정적 인 경우 클래스를 제거하고 네임 스페이스를 사용하지 않는 것이 좋습니다. 일부 기능을 비공개로 유지하려는 경우 헤더 파일에 게시하지 않습니다. –

+0

수정 된 답변보기 –

+0

"그냥 헤더 파일에 게시하지 않습니까?" – StAlRuth

답변

2

, 이것을 사용 :

Foo::addEntry("myId"); 

를 잊어 버린 질문에 대해 간단하게 static 키워드를 제거합니다.

+0

사실 내 질문을 잊어 버려 죄송합니다 ... – StAlRuth

관련 문제