Singleton Class error

This is a discussion on Singleton Class error within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; class Myclass { private: static int count; Myclass() { } public: static Myclass* CreateInstance(); ...

  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-28-2010 at 12:02 AM.

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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,789
    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
    Posts
    23,031
    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
    22,308
    You originally declared the static member variable but never defined it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,789
    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
    22,308
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    682
    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, 10: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, 05: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, 11:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21