我在使用靜態映射作爲C++成員時遇到了問題。我的頭文件是:靜態映射作爲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" ([email protected]@@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected][email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@@[email protected]@[email protected]@A)
什麼我可以做任何想法?
你不能在方法中定義靜態成員 –
如果我在類聲明後在頭文件中聲明它,我會得到相同的第一個錯誤。 – Kyle
[未定義的靜態類成員引用](http:// stackoverflow。com/questions/272900/undefined-reference-to-static-class-member) – juanchopanza