CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2010
    Posts
    7

    C++ Singleton Problem

    I have the code for a singleton below, and I am getting a linker error "multiple definitions " when i include the singleton.h file in more than one C++ file. This singleton code should cope with up to 5 singletons in a process and report error above that.

    The singleton clas is all in the .h file:


    class CMySingleton
    {
    private:
    CMySingleton(){}; // Private Constructor

    static int nCount; // Current number of instances
    static int nMaxInstance; // Maximum number of instances

    public:
    ~CMySingleton(); // Public Destructor

    static CMySingleton* CreateInstance(); // Construct Indirectly

    //Add whatever members you want
    };

    #endif // !defined(AFX_MYSINGLETON_H__8AA85488_875D_11DF_985E_0090F508B061__INCLUDED_)

    CMySingleton::~CMySingleton()
    {
    --nCount; // Decrement number of instances
    }


    CMySingleton* CMySingleton::CreateInstance()
    {
    CMySingleton* ptr = NULL;

    if(nMaxInstance > nCount)
    {
    ptr = new CMySingleton;
    ++nCount; // Increment no of instances
    }
    return ptr;
    }

    int CMySingleton::nCount = 0;
    int CMySingleton::nMaxInstance = 5; // When maxInstance is 1, we have a pure singleton class

    // end CMySingleton.h

    The multiple definition link errors occur on :
    CMySingleton::nCount
    int CMySingleton::nMaxInstance

    for eg.

    NOTE: This code works if i include it once , in main.cpp, in that case it compiles and links and runs.
    But i do need to include it in more than one *.cpp file:

    Any Advice appreciated.

    Thanks

    Marco

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++ Singleton Problem

    Move the definition part into a cpp file and leave just the declaration part in the header.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jul 2010
    Posts
    4

    Re: C++ Singleton Problem

    Hello Sir/madam~, I am find there are many links over google about the singleton, can you give me typical example of singletoon in windows XP coding Micosoft window?

    Thank you

  4. #4
    Join Date
    Jul 2010
    Posts
    7

    Re: C++ Singleton Problem

    Quote Originally Posted by S_M_A View Post
    Move the definition part into a cpp file and leave just the declaration part in the header.
    That is what I had in the first place, and it just would not compile or link then.

    I only got it working by moving everything into one header file. And it worked for one include into main.cpp;

    But when i created another test.cpp file to check whether it would limit the number of singletons as designed, it gave the link errors I originally posted.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: C++ Singleton Problem

    Try this: main.cpp
    Code:
    #include "singleton.h"
    // Didn't bother to create a header
    void CreateSingleton();
    void DeleteSingleton();
    int main(int argc, char* argv[])
    {
      CMySingleton* ptr = CMySingleton::CreateInstance();
      CreateSingleton();
      delete ptr;
      DeleteSingleton();
      return 0;
    }
    singleton.h
    Code:
    #ifndef _SINGLETON_H
    #define _SINGLETON_H
    
    class CMySingleton
    {
      private:
      CMySingleton(){}; // Private Constructor
    
      static int nCount; // Current number of instances
      static int nMaxInstance; // Maximum number of instances
    
    public:
      ~CMySingleton(); // Public Destructor
      static CMySingleton* CreateInstance(); // Construct Indirectly
    
    //Add whatever members you want
    };
    
    #endif
    singleton.cpp
    Code:
    #include "singleton.h"
    
    CMySingleton* CMySingleton::CreateInstance()
    {
      CMySingleton* ptr = NULL;
    
      if(nMaxInstance > nCount)
      {
        ptr = new CMySingleton; 
        ++nCount; // Increment no of instances
      }
      return ptr;
    }
    
    CMySingleton::~CMySingleton()
    {
      --nCount; // Decrement number of instances
    }
    
    int CMySingleton::nCount = 0;
    int CMySingleton::nMaxInstance = 5; // When maxInstance is 1, we have a pure singleton class
    otherfile.cpp
    Code:
    #include "singleton.h"
    
    static CMySingleton* ptr;
    void CreateSingleton()
    {
      ptr = CMySingleton::CreateInstance();
    }
    
    void DeleteSingleton()
    {
      delete ptr;
    }
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured