2013-09-25 37 views
4

我交流#developper要做C++的東西,我不明白這裏的問題:C++單嘗試:解析外部符號

namespace myNamespace 
{ 
    class Application 
    { 
    private: 
     Application(void); 
     ~Application(void); 

     // Not copyable 
     Application(const Application&); 
     Application& operator= (const Application&); 

     static Application _instance; 

     [...] 

    public: 
     static Application& current(void); 
    }; 
} 

(這應該是一個單身...)

,這將導致錯誤: 「錯誤LNK2001:無法解析的外部符號 」私人:靜態類了myNameSpace ::應用了myNameSpace ::應用:: _例如「(_instance @應用@了myNameSpace @@ 0V12 @阿?)」

是因爲我正在使用我在類聲明中聲明的類嗎?

非常感謝!

回答

12

你只聲明_instance在應用類,則需要在.cpp文件中定義它

namespace myNamespace 
{ 
    Application Application::_instance; 
} 

§9.4.2.2

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator.

+1

我的上帝,它的作品! !非常感謝。 (在例子'myNamespace :: Application myNamespace :: Application :: _ instance;'中) –