Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ singleton attempt: unresolved external symbol [duplicate]

Tags:

c++

singleton

I am a c# developper trying to do c++ things and I cannot understand the issue here:

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);
    };
}

(this is supposed to be a singleton...)

and this causes the error: "error LNK2001: unresolved external symbol "private: static class myNamespace::Application myNamespace::Application::_instance" (?_instance@Application@myNamespace@@0V12@A)"

Is it because I am using the class I am declaring in the class declaration?

Thanks a lot!

like image 236
Charles HETIER Avatar asked Sep 25 '13 12:09

Charles HETIER


1 Answers

You only declare _instance in Application class, you need to define it in .cpp file:

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.

like image 138
billz Avatar answered Oct 13 '22 06:10

billz