-
July 5th, 2010, 06:33 AM
#1
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
-
July 5th, 2010, 08:41 AM
#2
Re: C++ Singleton Problem
Move the definition part into a cpp file and leave just the declaration part in the header.
-
July 5th, 2010, 09:26 AM
#3
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
-
July 5th, 2010, 09:50 AM
#4
Re: C++ Singleton Problem
Originally Posted by S_M_A
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.
-
July 5th, 2010, 11:03 AM
#5
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|