Thread: Singleton Class error

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Singleton Class error

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Myclass
    {
    
    	private:	
    	static int count;
    	
    	
    	Myclass()
    	{
    
    	}
    
    
    	
    	
    	public:
    
    	static Myclass* CreateInstance();
    
    	~Myclass()
    	{
    		count--;
    
    	}
    };
    
     Myclass* Myclass::CreateInstance()
    	{
    		Myclass *ptr = NULL;		
    		if(!count)
    		{
    			count++;
    			ptr = new Myclass;
    			return( ptr );
    		}
    		else
    		{
    			cout << "\nClass Instantiation no possible!\n";
    			return NULL;
    		}
    	}
    
    
    int main()
    {
    		Myclass *ob = Myclass :: CreateInstance();
    
    		return 0;
    }
    ERROR MESSAGE..

    Code:
    /tmp/ccPSDOBz.o: In function `Myclass::CreateInstance()':
    test.cpp:(.text+0x13): undefined reference to `Myclass::count'
    test.cpp:(.text+0x1d): undefined reference to `Myclass::count'
    test.cpp:(.text+0x26): undefined reference to `Myclass::count'
    collect2: ld returned 1 exit status
    Last edited by anirban; 10-27-2010 at 11:02 PM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    And the error is......?

  3. #3
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Sorry forgot to mention the error!
    Now edited..

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Declare it outside of the class declaration, in the cpp file as you did with the CreateInstance function

    >>int Myclass::count;

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are creating a memory leak by not cleaning up properly. Don't forget to initialize count.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Quote Originally Posted by Fordy View Post
    Declare it outside of the class declaration, in the cpp file as you did with the CreateInstance function

    >>int Myclass::count;
    Okay problem solved but what is the reason behind such an error.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You originally declared the static member variable but never defined it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    As laserlight says, you need to define a static member variable outside of the class declaration.

    >>You are creating a memory leak by not cleaning up properly.

    Come on...its hardly production level code. If you want to be particularly pedantic, he didnt try to catch bad_alloc after the call to new....

    >>Don't forget to initialize count.

    A static int will be zero-initialized anyway, but it doesn't hurt to be explicit

  9. #9
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    Quote Originally Posted by laserlight View Post
    You originally declared the static member variable but never defined it.
    So for every static variable do I have to do such outside class ?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anirban
    So for every static variable do I have to do such outside class ?
    For all static member variables, yes, except for static const member variables of integer types as they can be defined in the class definition by initialising them there.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I would place count++ in the class constructor, so if the constructor throws an exception and the object is not created, the count remains valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM