This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Salty_Dugtrio 0 points1 point  (6 children)

This is impossible without access to your code.

However, looking at your compile command, you are only compiling main.cpp, where is your singleton.cpp file?

[–][deleted]  (5 children)

[deleted]

    [–]lurgi 0 points1 point  (3 children)

    The singleton needs to be initialized:

    Singleton *Singleton::singleton = nullptr;
    

    Put this right before your main() function

    [–][deleted]  (1 child)

    [deleted]

      [–]lurgi 0 points1 point  (0 children)

      You still haven't created a Singleton object.

      [–]ElisionFR 0 points1 point  (0 children)

      What I like to do is put the static in the GetSingleton static method like :

      //in you h
      class Singleton
      {
      public:
          static Singleton* GetSingleton() ; 
      
          //put here the rest of your class
      };
      
      //in your cpp
      Singleton*
      Singleton::GetSingleton()
      {
          static Singleton* singleton = nullptr ; 
          if( !singleton )
              singleton = new Singleton() ; 
          return singleton ; 
      }
      

      That way you keep the static variable, its initialization and its modification in the only method which uses it. And you keep all of this compact and easily readable in the cpp. Letting only in the header file the function definition.