Singleton (design pattern)

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Singleton (design pattern)

      Hi,

      I am writting a class that uses singletons, but I am
      running into a link problem. I have searched the Internet
      and came up with some issues regarding the error, but
      none specific. So, I decided to have you guys look at
      it and see if there is something that I am doing wrong.

      Here is the error message:

      Source Code

      1. Singleton error LNK2001: unresolved external symbol "private: __thiscall CSingleton::CSingleton(void)" (??0CSingleton@@$$FAAE@XZ)


      CSingleton.h

      Source Code

      1. class CSingleton
      2. {
      3. private:
      4. static CSingleton *m_pInstance;
      5. CSingleton();
      6. CSingleton( const CSingleton &s);
      7. CSingleton &operator =( const CSingleton &rhs );
      8. ~CSingleton();
      9. protected:
      10. public:
      11. static CSingleton *GetInstance( void );
      12. void PrintMessage( void );
      13. };
      Display All



      "CSingleton.h"

      Source Code

      1. #include "CSingleton.h"
      2. CSingleton *CSingleton::m_pInstance = NULL;
      3. CSingleton::~CSingleton()
      4. {
      5. }
      6. CSingleton *CSingleton::GetInstance( void )
      7. {
      8. if ( m_pInstance == NULL )
      9. {
      10. m_pInstance = new CSingleton(); // BAD way of doing this.
      11. // m_pInstance = std::auto_ptr<CSingleton> CSingleton; // MUCH better way!
      12. }
      13. return ( m_pInstance );
      14. }
      15. void CSingleton::PrintMessage( void )
      16. {
      17. std::cout << "Hello there singleton!" << std::endl;
      18. }
      Display All



      Main.cpp

      Source Code

      1. #include "CSingleton.h"
      2. void main( void )
      3. {
      4. // CSingleton &s = CSingleton::GetInstance();
      5. CSingleton *s1, *s2;
      6. s1 = CSingleton::GetInstance();
      7. s2 = CSingleton::GetInstance();
      8. s1->PrintMessage();
      9. s2->PrintMessage();
      10. }
      Display All



      Ok, so what I am not seeing that is causing the LNK
      error?! From several sources that I have been pulling
      from off the Internet, this is the "right way", right?



      Thanks again,

      Sabrina.
    • first, my guess is that you are using Visual Studio. I've done singleton in VS.NET and it likes to give me linker errors because of the private constructor.

      What you probibly need to do is to set your constructor to protected and derive a class from your singleton class. I'm not sure, you may need to change your code to accomidate the subclassing.
      .Code
      push you ; haha!
    • Version 6 of Visual C++ had problems like this
      I generally settled for a logical singleton putting assertion code in the constructor

      CSingleton *CSingleton::m_pInstance = NULL;

      CSingleton::~CSingleton()
      {
      if (m_pInstance != NULL)
      {
      assert(false); // somebody's making a second one
      DebugBreak(); // break into debugger on Windows
      }
      }
    • Hi Sabrina,

      Where is your definition of the default constructor?

      You are declaring CSingleton(); in Singleton.h, but I see no definition of this default constructor in Singleton.cpp.

      Source Code

      1. CSingleton::CSingleton()
      2. {
      3. //default constructor does nothing, but still needs to be defined.
      4. }