我在使用静态地图作为 C++ 成员时遇到问题。我的头文件是:
class Article
{
public:
//
static map<string,Article*> dictionary;
....
....
};
在我的构造函数中,我首先调用以下方法:
void Article::InitializeDictionary()
{
#ifndef DICT
#define DICT
map<string,Article*> Article::dictionary;
#endif
}
基于有关此的其他帖子,我应该声明静态成员,但是当我尝试这样做时,出现以下错误:
Error 1 error C2655: 'Article::dictionary' : definition or redeclaration illegal in current scope c:\.......\article.cpp 88 1
如果我将功能更改为以下内容:
void Article::InitializeDictionary()
{
#ifndef DICT
#define DICT
Article::dictionary["null"] = 0;
#endif
}
我收到此错误:
Error 1 error LNK2001: unresolved external symbol "public: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class Article *,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class Article *> > > Article::dictionary" (?dictionary@Article@@2V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVArticle@@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVArticle@@@std@@@2@@std@@A)
关于我能做什么的任何想法?