Thread: Singleton problems

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    35

    Singleton problems

    I posted a question similar to this one before, but it seemed to die out.

    Here is my problem: I have two classes using a singleton design pattern, and everything works fine. For our purposes one of the classes is called GameManager. The problem comes when I try to get the static instance of the GameManager from another class, let's say, Camera. I get a lot of parse errors before "*" from g++. Here are the parts of Camera and GameManager that i believe are causing problems:

    GameManager.h

    Code:
    #ifndef AC_GAMEMANAGER_H_
    #define AC_GAMEMANAGER_H_
    
    #ifdef _WINDOWS
    #include <windows.h>
    #endif
    
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <math.h>
    
    #include "SDL.h"
    #include "types.h"
    #include "UIManager.h"
    #include "Error.h"
    #include "Scene.h"
    #include "Keys.h"
    
    class GameManager
    {
        static GameManager *_instance;
        UIManager *uimanager;
        Scene scene;
        Keys keys;
        
    
        ac_int init(ac_void);
        ac_void loopGame(ac_void);
        ac_void renderScene(ac_void);
        
        GameManager() {}
    public:
        static GameManager *instance(ac_void);
    
        ac_void beginGameLoop(ac_void);
    
        
        /* Get and Set */
         
         /**
         * @return the <code>Scene</code> object
         */   
        Scene getScene(ac_void) const { return scene; }
        
        /**
         * @return the <code>Keys</code> object
         */    
        Keys getKeys(ac_void) { return keys; }
    };
    
    
    #endif /* AC_GAMEMANAGER_H_ */
    And the instance function for the GameManager:

    Code:
    GameManager *GameManager::_instance = 0;
    
    GameManager *GameManager::instance()
    {
        if (!_instance)
        {	
            _instance = new GameManager();
            _instance->init();
        }
        
        return _instance; 
    }
    Here are the problem parts in Camera:

    Camera.h

    Code:
    #ifndef AC_CAMERA_H_
    #define AC_CAMERA_H_
    
    #include <GL/gl.h>
    #include <GL/glu.h>
    
    #include <stdio.h>
    #include "SDL.h"
    #include <math.h>
    
    #include "UIManager.h"
    #include "GameManager.h"
    #include "MainFrame.h"
    #include "Keys.h"
    #include "Error.h"
    #include "version.h"
    
    class Camera {
    /* ... */   
        
        GameManager *gameManager;
    public:
    /* ... */
    };
    #endif
    And the places I use the gameManager in the Camera.cpp file:
    Code:
    Camera::Camera()
    {
    /* ... */
        
        gameManager = GameManager::instance();
    }
    
    /** And inside a movement function: */
        if(gameManager->getKeys().isActionDown("+forward")) {             
    
            MoveCamera(speed);              
        }
    Here is the compiler output:

    Code:
    cd '/home/max/programming/c++/gl/angelchronicles/angelchronicles/debug' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" gmake -k -j1 
    compiling angelchronicles.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Camera.h:104: error: syntax error before `*' token
    compiling LoadManager.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Camera.h:104: error: syntax error before `*' token
    compiling Game.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Camera.h:104: error: syntax error before `*' token
    compiling GameManager.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Camera.h:104: error: syntax error before `*' token
    compiling Camera.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:29: error: ' Camera' is used as a type, but is not defined as a type.
    compiling Scene.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:43: error: ' Scene' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:63: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:68: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:68: error: syntax error before `(' token
    compiling GameSceneRender.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:51: error: syntax error before `*' token
    *** Exited with status: 2 ***
    Note: The problem goes away when I don't use GameManager in Camera.

    Thanks alot, and if you need more code just ask. Any suggestions? Thanks!
    you make me rery ascared

  2. #2
    Bob Dole for '08 B0bDole's Avatar
    Join Date
    Sep 2004
    Posts
    618
    >The problem goes away when I don't use GameManager in Camera.

    then don't
    Hmm

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Well that would be the easy way out, but then I don't get the functionality that I need. But is there a reason that this is happening? Am I approaching the singleton in the wrong way? Thanks.
    you make me rery ascared

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    try placing this line in GameManager.h instead of GameManager.cpp:

    >> GameManager *GameManager::_instance = 0;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    No change, I am afraid to say.

    Could it possibly be that I have the first instance of GameManager created in a non-class as a global?

    Here is my game.h file:

    Code:
    #ifndef AC_GAME_H_
    #define AC_GAME_H_
    
    #include "types.h"
    #include "UIManager.h"
    #include "GameManager.h"
    
    extern UIManager *uimanager;
    extern GameManager *gamemanager;
    
    /**
     * Start the game by initializing the UIManager and the
     * GameManager, the two classes that handle the majority of
     * the game.  
     * @return any init codes
     */
    
    ac_int startGame(ac_void);
    
    
    
    #endif /* AC_GAME_H_ */
    And Game.cpp
    Code:
    #include "Game.h"
    #include "Error.h"
    
    UIManager *uimanager;
    GameManager *gamemanager;
    /* Start the whole game.  Begin
     * by initializing the UIManager, then
     * the GameManager while will begin the event
     * loop.
     */
     
    ac_int startGame(ac_void)
    {
    	//UIManager *uimanager;
    	//GameManager *gamemanager;
    
    	uimanager = UIManager::instance();
    	gamemanager = GameManager::instance();
    
    	gamemanager->beginGameLoop();
    
    	return AC_INIT_SUCCESS;
    }
    you make me rery ascared

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Could it possibly be that I have the first instance of GameManager created in a non-class as a global?

    that shouldn't matter. I even wrote a test application to duplicate the problem, but everything worked fine. not sure what the problem is...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Here is a package of all the code I have right now, maybe I am missing something else where...There are makefiles and everything included for linux, the win32 stuff is old in the package. Here is the link:
    http://max.muffinpeddler.com/arch/an...200511.tar.bz2

    Thanks so much for your help.
    Last edited by techrolla; 01-01-2005 at 03:36 PM.
    you make me rery ascared

  8. #8
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Have you tested if it works on any other compilers?

  9. #9
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Another thing, but thats not a error I think, just me who dont know much about C++

    but this sentence:

    GameManager() {}

    What does it do?
    It looks to me like its calling the function but.. what does the two block-marks do? ( { } )
    and why dont they need a ";" after them to finish the sentence?



    EDIT:
    Hmm..wait wait wait.. Its a function declaration?
    Last edited by Da-Nuka; 01-01-2005 at 04:40 PM.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    The GameManager() {} just defines the default constructor, but it doesn't do anything. I may add to it later, but for now no.

    Here is the vc++ 6 compiler output. Please disregard the opengl header errors, I need to add windows.h to the files:

    Code:
    --------------------Configuration: win32 - Win32 Debug--------------------
    Compiling...
    angelchronicles.cpp
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2143: syntax error : missing ';' before '*'
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'GameManager' : missing storage-class or type specifiers
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'gameManager' : missing storage-class or type specifiers
    Error.cpp
    Game.cpp
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2143: syntax error : missing ';' before '*'
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'GameManager' : missing storage-class or type specifiers
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'gameManager' : missing storage-class or type specifiers
    LoadManager.cpp
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2143: syntax error : missing ';' before '*'
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'GameManager' : missing storage-class or type specifiers
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'gameManager' : missing storage-class or type specifiers
    MainFrame.cpp
    Prefs.cpp
    UIManager.cpp
    Camera.cpp
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\gamescenerender.h(29) : error C2146: syntax error : missing ';' before identifier 'camera'
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\gamescenerender.h(29) : error C2501: 'Camera' : missing storage-class or type specifiers
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\gamescenerender.h(29) : error C2501: 'camera' : missing storage-class or type specifiers
    FreeType.cpp
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\freetype.cpp(364) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<
    char>,std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug 
    information
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\freetype.cpp(364) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,
    std::allocator<char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
    GameManager.cpp
    c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
     >::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
    c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
     >::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2143: syntax error : missing ';' before '*'
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'GameManager' : missing storage-class or type specifiers
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\camera.h(106) : error C2501: 'gameManager' : missing storage-class or type specifiers
    GameSceneRender.cpp
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing ';' before type 'void'
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: 'WINGDIAPI' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file found
    Keys.cpp
    c:\documents and settings\max\desktop\angelchronicles\angelchronicles\src\keys.cpp(155) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    LoadingRender.cpp
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing ';' before type 'void'
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: 'WINGDIAPI' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file found
    MenuRender.cpp
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing ';' before type 'void'
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: 'WINGDIAPI' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file found
    Scene.cpp
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2144: syntax error : missing ';' before type 'void'
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : error C2501: 'WINGDIAPI' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\vc98\include\gl\gl.h(1152) : fatal error C1004: unexpected end of file found
    version.cpp
    Error executing cl.exe.
    
    win32.exe - 27 error(s), 5 warning(s)
    you make me rery ascared

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Code:
    static GameManager *instance(ac_void); // ?
    ...
    GameManager *GameManager::instance()
    {
    ...
    What is 'ac-void' out of curiosity? Is it defined somewhere as void or something else? Anyway, since there aren't errors regarding that, I presume it is.

    My guess is that the problem has to do with the following errors:
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:43: error: ' Scene' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:63: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:68: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:68: error: syntax error before `(' token
    compiling GameSceneRender.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:51: error: syntax error before `*' token
    Which points strongly toward the problem being with Scene.

    *edit*
    What also makes me suspicious of that is that you are getting errors on lines 63 and 68 of a 52 line file (unless you didn't post all of GameManager.h).


    Cheers,
    Zach
    Last edited by Zach L.; 01-01-2005 at 09:02 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Thanks I will look into it. I want to believe that the error is from Camera, a problem with my singleton implementation, but you may be right. If you don't mind, I gave a link above to the snapshot full code archive that has everything. Thanks alot for helping!

    Edit: After scanning through Scene, I don't notice anything of worry.

    And ac_void is typedef'ed as void.

    Oh yea, and the "nonexistent" file line numbers are because I left out the common GPL heading stuff, which is about 15 lines
    Last edited by techrolla; 01-01-2005 at 10:41 PM.
    you make me rery ascared

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Heres the definition of the error I found online:
    C2143: syntax error : missing ';' before '*'

    If this error is followed by two C2501 errors then the problem is an undeclared class name within a pointer declaration.

    For example, the declaration:

    CClass *pObject;

    will generate the above error message followed by a C2501 error message for 'CClass' and another C2501 message for 'pObject'. The problem is that the compiler isn't recognizing CClass as a valid class/type name. To correct the problem add a #include of the file containing the declaration of CClass (e.g., #include CClass.h)
    The problem is, I have the proper header included...
    you make me rery ascared

  14. #14
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    static GameManager *_instance;
    UIManager *uimanager;
    Scene scene;
    Keys keys;


    ac_int init(ac_void);
    ac_void loopGame(ac_void);
    ac_void renderScene(ac_void);


    Those functions and such... What happens to them when they arent defined in "public:" nor "private:"?
    ----------------------------------------------------------------------------
    also, i dont know if it help, but have you tried to add
    "using namespace std;" in your class-headers?

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Those functions and such... What happens to them when they arent defined in "public:" nor "private:"?
    They are automatically made private in a class, or public in a struct (so, private in this case).

    Nothing appears to be wrong with the singleton implementation itself. And the initialization of _instance is perfectly fine going in the source file.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singleton template problem
    By cloudy in forum C++ Programming
    Replies: 14
    Last Post: 01-11-2009, 05:40 AM
  2. Singleton Problems..
    By JJFMJR in forum C++ Programming
    Replies: 8
    Last Post: 04-11-2007, 05:08 PM
  3. How To Derive from a singleton
    By appleGuy in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2007, 01:55 PM
  4. Thread-safe singleton.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2006, 10:45 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM