Thread: Possible circular definition with singleton objects

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

    Possible circular definition with singleton objects

    I am coding a game, and I have a class that I have created into a singleton object. That singleton object declares some variables, and everything works just fine. The problem comes when one of those variables refers to the singleton object, most likely through its instance() function. Note, to the best of my knowledge my problem isn't from a lack of inclusion gaurds in my headers, which I have. Anyways, when they refer to each other, I get a crazy slew of errors from g++ telling me that the types defined in the singleton class are not really types. 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 Camera.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/types.h:35:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/version.h:46:27: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/ScreenConstants.h:37:35: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/MainFrame.h:96:29: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Error.h:50:25: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/UIManager.h:70:1: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:31: error: ' Camera' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:41:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:70:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:44: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:57: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: syntax error before `(' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:63:31: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    compiling Keys.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/types.h:35:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/version.h:46:27: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/ScreenConstants.h:37:35: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/MainFrame.h:96:29: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Error.h:50:25: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/UIManager.h:70:1: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:41:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:70:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:60: error: parse error before `}' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:63:31: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    compiling Scene.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/types.h:35:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Error.h:50:25: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/version.h:46:27: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/ScreenConstants.h:37:35: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/MainFrame.h:96:29: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/UIManager.h:70:1: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:70:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:44: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:45: error: ' Scene' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:57: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: syntax error before `(' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:63:31: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:41:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:70:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.cpp:62:2: warning: no newline at end of file
    compiling GameSceneRender.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/types.h:35:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/version.h:46:27: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/ScreenConstants.h:37:35: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/MainFrame.h:96:29: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Error.h:50:25: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/UIManager.h:70:1: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:41:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:38: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:70:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:44: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:57: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: syntax error before `(' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:63:31: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Keys.h:99:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:41:7: warning: no newline at end of file
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.cpp:143:2: warning: no newline at end of file
    *** Exited with status: 2 ***
    The errors come more towards the end when the offending file, GameManager is compiled.

    Here is an example of what would cause this error:

    Having an instance of the Keys class in GameManager, but then having the Keys class get the singleton object of the GameManager and referencing it from there.

    Any suggestions? Am I not implementing this singleton object correctly? Thanks!
    you make me rery ascared

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You know you could get rid of the majoirty of those errors (thus making it easier to read) by putting a newline at the end of the file.

    Without seeing any source code its impossible to give an exact answer.
    Last edited by Thantos; 12-25-2004 at 10:35 PM. Reason: Realized they weren't seperate cpp files but seperate h files

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Hey thanks for replying, here are the snippets of the problem files:

    Here is the Keys class
    Code:
    #ifndef AC_KEYS_H_
    #define AC_KEYS_H_
    
    /** A key-binding implementation.  
     * Problems: 
     * There is a limit to bindings for a key (linked-list?)
     * it is experimental, probably won't do it like this
     * later on, but for now she works!
     */
    
    #include "types.h"
    #include "SDL.h"
    /**
     * FIXME: I Don't think we want keys to have to talk
     * to GameManager, it should be the other way around!
     */
    #include "GameManager.h"
    typedef int keystate_t;
    
    #define AC_KEY_STATE_DOWN 1
    
    //Up with action associated with
    #define AC_KEY_STATE_UP_ACTION 2
    
    //Up, but with no action (resting)
    #define AC_KEY_STATE_UP 0
    
    #define AC_LEFT 1
    #define AC_RIGHT 2
    #define AC_FORWARD 3
    #define AC_BACK 4
    
    /** Hold a SDLKey which will hold the actual key pressed, and
      * the state of the key.
      */
    typedef struct {
        SDLKey key;
        keystate_t keystate;
    } keyent_t;
    
    /** A structure that holds key entries that bind to 
      * the current required directions and keys for the game.
      */
    typedef struct {
        keyent_t left;
        keyent_t right;
        keyent_t forward;
        keyent_t back;
    } keymap_t;
    
    class Keys
    {
        keymap_t keymap;
        
    public:
        Keys() {}
        
        ac_void initKeys(ac_void);
        ac_void handleKeyPress(SDL_keysym keysym);
        ac_void handleKeyRelease(SDL_keysym keysym);     
    
        ac_void setKeyState(SDLKey, keystate_t keystate);
    
        /**
         * Create a key entry that has a specific key
         * bound to it.
         *
         * @param key the SDL defined key value
         * @param keystate the integral key state of the key
         * @return the newly created keyentry
         */
        keyent_t makeKey(SDLKey key, keystate_t keystate);
        
        ac_int getKeyValue(ac_int keyid) const;
        
        ac_boolean isKeyPressed(ac_int keyname) const;
    };
    
    #endif
    And the GameManager class:
    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 "Camera.h"
    #include "Scene.h"
    #include "Keys.h"
    
    class GameManager
    {
        static GameManager *_instance;
        UIManager *uimanager;
        Camera *camera;
        Scene scene;
        Keys keys;
        
        ac_int init(ac_void);
        ac_void loopGame(ac_void);
        ac_void renderScene(ac_void);
    public:
        static GameManager *instance(ac_void);
    
        ac_void beginGameLoop(ac_void);
        
        Camera *getCamera(ac_void) const { return camera; }
        Scene getScene(ac_void) const { return scene; }
        Keys getKeys(ac_void) { return keys; }
    };
    
    
    #endif /* AC_GAMEMANAGER_H_ */
    Here is the cleaner 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++)
    compiling LoadManager.cpp (g++)
    compiling UIManager.cpp (g++)
    compiling MainFrame.cpp (g++)
    compiling Game.cpp (g++)
    compiling Error.cpp (g++)
    compiling GameManager.cpp (g++)
    compiling Prefs.cpp (g++)
    compiling version.cpp (g++)
    compiling Camera.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameSceneRender.h:31: error: ' Camera' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:44: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:57: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: syntax error before `(' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    compiling Keys.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:60: error: parse error before `}' token
    compiling Scene.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:44: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:45: error: ' Scene' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:57: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: syntax error before `(' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    compiling GameSceneRender.cpp (g++)
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/Scene.h:38: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:44: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:46: error: ' Keys' is used as a type, but is not defined as a type.
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:57: error: syntax error before `*' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: ISO C++ forbids defining types within return type
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:58: error: syntax error before `(' token
    /home/max/programming/c++/gl/angelchronicles/angelchronicles/src/GameManager.h:59: error: parse error before `)' token
    *** Exited with status: 2 ***
    Note: This seems to happen with some other classes as well, such as Camera, etc., but the problem seems to be the same as in Keys and GameManager. Any suggestions? Need more code? Thanks!
    you make me rery ascared

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    35
    Well whenever I take out all the GameManager includes and references in Keys, everything compiles fine. Maybe this is an automake conflict? I don't really know. Thanks.
    you make me rery ascared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM