Thread: Compiler complaining about my singleton class

  1. #1
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743

    Compiler complaining about my singleton class

    The compiler is complaining about my singleton class I am creating. It's pretty simple.

    Here is the .h file:

    Code:
    class GAME_MANAGER
    {
    	private:
    
    		//Single instance of the GAME_MANAGER class
    		static GAME_MANAGER *gameManagerInstance;
    
    		//Constructors and assignment operator - private
    		GAME_MANAGER ( ) { };
    		GAME_MANAGER ( const GAME_MANAGER &copy ) { };
    		void operator = ( const GAME_MANAGER &rhs ) { };
    
    	public:
    
    		static GAME_MANAGER *GetInstance ( );
    
    };
    Here is the cpp file:

    Code:
    #include "game_manager.h"
    
    GAME_MANAGER *GAME_MANAGER::gameManagerInstance = NULL;
    
    static GAME_MANAGER::GAME_MANAGER *GetInstance ( )
    {
    	if ( !gameManagerInstance )
    	{
    		gameManagerInstance = new GAME_MANAGER();
    	}
    
    	return gameManagerInstance;
    }
    As far as I know this is the way singletons are supposed to be done in C++, but it is really complaining at me a lot.

    The errors are as follows:
    game_manager.cpp||In function `GAME_MANAGER* GetInstance()':|
    game_manager.cpp|7|error: `gameManagerInstance' was not declared in this scope|
    inc\game_manager.h|25|error: `GAME_MANAGER::GAME_MANAGER()' is private|
    game_manager.cpp|9|error: within this context|
    game_manager.cpp|12|error: `gameManagerInstance' was not declared in this scope|
    ||=== Build finished: 4 errors, 0 warnings ===|
    Any ideas what is going wrong?

    Oh...I am using Code::Blocks, and the compiler appears to be GCC 3.4.5.

    Thanks
    Last edited by DavidP; 06-07-2009 at 11:55 PM.
    My Website

    "Circular logic is good because it is."

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This:
    Code:
    static GAME_MANAGER::GAME_MANAGER *GetInstance ( )
    should be:
    Code:
    GAME_MANAGER *GAME_MANAGER::GetInstance ( )
    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

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    oops

    thanks for catching that
    My Website

    "Circular logic is good because it is."

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You may want to also consider changing your declaration to:
    Code:
    static std::auto_ptr<GAME_MANAGER> gameManagerInstance;
    This way you don't leak memory when the application exits. It may not be too big of a deal if the class doesn't do anything critical in the destructor, but it's always nice to have memory leak tools report that your application is leak free.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, this:
    Code:
    void operator = ( const GAME_MANAGER &rhs ) { };
    should be this:
    Code:
    GAME_MANAGER& operator = ( const GAME_MANAGER &rhs ) { };
    And why all capital letters for the class name? That ugly naming convention is usually used for macros.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    Also, this:
    Code:
    void operator = ( const GAME_MANAGER &rhs ) { };
    should be this:
    Code:
    GAME_MANAGER& operator = ( const GAME_MANAGER &rhs ) { };
    In this case it makes no difference since either way the copy assignment operator is disabled.
    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

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by laserlight View Post
    In this case it makes no difference since either way the copy assignment operator is disabled.
    I was wondering about that. Does it make any sense to have either a copy constructor or an overloaded asssignment operator in a singleton class? (The assignment in GetInstance() is just an assignment of a pointer & doesn't need an overloaded =.)

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    Does it make any sense to have either a copy constructor or an overloaded asssignment operator in a singleton class?
    Probably not, since copying implies that you have more than one object.

    Incidentally, the copy constructor and copy assignment operator do not have to be defined in order to be disabled.
    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

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Indeed, you should remove the implementations of copying and assignment, so that if you accidentally try to copy where they are even accessible you still get linker errors.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    And why all capital letters for the class name? That ugly naming convention is usually used for macros.
    Well, when I code in C++ my normal IDE is either Notepad++ or Code::Blocks. Consequently, class names are not highlighted any special color when I declare objects (Visual Studio, on the other hand, highlights the name of the class when you declare an instance of it...but I mostly only use Visual Studio for C#).

    Consequently, to distinguish between variable names and class names in my C++ code I use all caps quite often for my class names. It makes the class name easier to distinguish from the rest of your code in an IDE that doesn't highlight them for you.
    My Website

    "Circular logic is good because it is."

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Then how do you distinguish class names from macros?

    I just start class names with a capital letter and variables with a small letter...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Well, my macros are usually easily distinguishable by what they are.

    For example:

    AGENT_COUNT_DEFAULT

    is obviously a macro (defined to 100 if you were wondering )

    whereas:

    GAME_MANAGER

    is obviously a class.

    I sometimes start out variables with a capital letter depending on what the variable is, but most of my variables start out with lowercase letters like you do.
    My Website

    "Circular logic is good because it is."

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I like to start the classes with a letter identifying the type, eg C. CGameManager. Then I know it's a class no matter what I name the instance of it.
    But... you do know that you shouldn't really use macros in C++, yes? A constant integer/float/whatever will do the trick instead.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM