Cyclic dependency

Discussion in 'C++' started by Nick Forrington, Nov 16, 2004.

  1. Hi, was wondering if anyone could help me out here, either by someone
    telling me how to do what I'm trying to do or by letting me know it
    can't be done...

    I've got a Game class and an Entity class, the Game has a number of
    entities in it, and I want the entity to have a reference to the game class.

    The compiler obviously doens't like this. I read someone where about a
    solution of just declaring the Game class "class Game;" without
    including the file, or vice versa, but this just makes the compiler
    complain when I try to access a method of the Game class.

    Any ideas? can this even be done?

    Thanks in advance
    Nick
     
    Nick Forrington, Nov 16, 2004
    #1
    1. Advertising

  2. Nick Forrington wrote:
    > Hi, was wondering if anyone could help me out here, either by someone
    > telling me how to do what I'm trying to do or by letting me know it
    > can't be done...
    >
    > I've got a Game class and an Entity class, the Game has a number of
    > entities in it, and I want the entity to have a reference to the game
    > class.
    >
    > The compiler obviously doens't like this. I read someone where about a
    > solution of just declaring the Game class "class Game;" without
    > including the file, or vice versa, but this just makes the compiler
    > complain when I try to access a method of the Game class.


    Don't access methods until you define Game class completely.

    > Any ideas? can this even be done?


    Sure. Put the member function definitions after both classes have been
    fully defined. Get out of Java habit of defining all member functions
    in the class definition.

    class Game;

    class Entity {
    Game* pgame;
    public:
    void useGame();
    };

    class Game {
    std::list<Entity> entitylist;
    public:
    void move();
    void doSomeEntityStuff(Entity* pe);
    };

    void Entity::useGame() {
    pgame->doSomeEntityStuff(this);
    }

    void Game::move() { // blahblah
    }


    V
     
    Victor Bazarov, Nov 16, 2004
    #2
    1. Advertising

  3. Nick Forrington

    Ron Natalie Guest

    Nick Forrington wrote:
    > Hi, was wondering if anyone could help me out here, either by someone
    > telling me how to do what I'm trying to do or by letting me know it
    > can't be done...
    >
    > I've got a Game class and an Entity class, the Game has a number of
    > entities in it, and I want the entity to have a reference to the game
    > class.
    >
    > The compiler obviously doens't like this. I read someone where about a
    > solution of just declaring the Game class "class Game;" without
    > including the file, or vice versa, but this just makes the compiler
    > complain when I try to access a method of the Game class.
    >

    Take all the manuals off your desk, pile them on top of your chair so
    you can see over the top of your cubicle walls and shout "Does anybody
    know how to read a FAQ?"


    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-38.11

    You can use the forward decalration to declare the classes, but before
    you actually use the members, you have to fully declare the class.

    class Game;
    class Entity {
    public:
    Entity(Game* g);

    private:
    Game* my_game;
    };

    class Game {
    public:
    void Member();
    };

    Entity::Entity(Game* g) : my_game(g) {
    my_game->Member();
    }
     
    Ron Natalie, Nov 16, 2004
    #3
  4. Victor Bazarov wrote:
    > Nick Forrington wrote:
    >
    >> Hi, was wondering if anyone could help me out here, either by someone
    >> telling me how to do what I'm trying to do or by letting me know it
    >> can't be done...
    >>
    >> I've got a Game class and an Entity class, the Game has a number of
    >> entities in it, and I want the entity to have a reference to the game
    >> class.
    >>
    >> The compiler obviously doens't like this. I read someone where about a
    >> solution of just declaring the Game class "class Game;" without
    >> including the file, or vice versa, but this just makes the compiler
    >> complain when I try to access a method of the Game class.

    >
    >
    > Don't access methods until you define Game class completely.
    >
    >> Any ideas? can this even be done?

    >
    >
    > Sure. Put the member function definitions after both classes have been
    > fully defined. Get out of Java habit of defining all member functions
    > in the class definition.
    >
    > class Game;
    >
    > class Entity {
    > Game* pgame;
    > public:
    > void useGame();
    > };
    >
    > class Game {
    > std::list<Entity> entitylist;
    > public:
    > void move();
    > void doSomeEntityStuff(Entity* pe);
    > };
    >
    > void Entity::useGame() {
    > pgame->doSomeEntityStuff(this);
    > }
    >
    > void Game::move() { // blahblah
    > }
    >
    >
    > V


    Thanks for the reply V

    At the minute I've got a Game.h, Game.cpp, BaseEntity.h and
    BaseEntity.cpp files, would I need to arrange it all so they're in the
    same file then?

    I'm using eclipse with the CDT plugin, which might be bad for doing this
    kind of thing.

    Thanks
    Nick
     
    Nick Forrington, Nov 16, 2004
    #4
  5. Nick Forrington wrote:
    > Victor Bazarov wrote:
    >
    >> Nick Forrington wrote:
    >>
    >>> Hi, was wondering if anyone could help me out here, either by someone
    >>> telling me how to do what I'm trying to do or by letting me know it
    >>> can't be done...
    >>>
    >>> I've got a Game class and an Entity class, the Game has a number of
    >>> entities in it, and I want the entity to have a reference to the game
    >>> class.
    >>>
    >>> The compiler obviously doens't like this. I read someone where about
    >>> a solution of just declaring the Game class "class Game;" without
    >>> including the file, or vice versa, but this just makes the compiler
    >>> complain when I try to access a method of the Game class.

    >>
    >>
    >>
    >> Don't access methods until you define Game class completely.
    >>
    >>> Any ideas? can this even be done?

    >>
    >>
    >>
    >> Sure. Put the member function definitions after both classes have been
    >> fully defined. Get out of Java habit of defining all member functions
    >> in the class definition.
    >>

    ------------------- this would be your [Base]Entity.h
    >> class Game;
    >>
    >> class Entity {
    >> Game* pgame;
    >> public:
    >> void useGame();
    >> };

    ------------------- this would be your Game.h
    #include <Entity.h>
    >>
    >> class Game {
    >> std::list<Entity> entitylist;
    >> public:
    >> void move();
    >> void doSomeEntityStuff(Entity* pe);
    >> };

    ------------------- this would be your [Base]Entity.cpp
    #include <Entity.h>
    #include <Game.h>
    >>
    >> void Entity::useGame() {
    >> pgame->doSomeEntityStuff(this);
    >> }
    >>

    ------------------- this would be your Game.cpp
    #include <Game.h>
    #include <Entity.h>
    >> void Game::move() { // blahblah
    >> }

    ---------------------------------------------------
    >>
    >>
    >> V

    >
    >
    > Thanks for the reply V
    >
    > At the minute I've got a Game.h, Game.cpp, BaseEntity.h and
    > BaseEntity.cpp files, would I need to arrange it all so they're in the
    > same file then?


    No.

    > I'm using eclipse with the CDT plugin, which might be bad for doing this
    > kind of thing.


    I don't know what 'eclipse' or 'CDT plugin' are, sorry.

    V
     
    Victor Bazarov, Nov 16, 2004
    #5
  6. Nick Forrington

    Jason Heyes Guest

    "Nick Forrington" <> wrote in message
    news:cne0v8$nam$...
    > Thanks for the reply V
    >
    > At the minute I've got a Game.h, Game.cpp, BaseEntity.h and BaseEntity.cpp
    > files, would I need to arrange it all so they're in the same file then?
    >
    > I'm using eclipse with the CDT plugin, which might be bad for doing this
    > kind of thing.
    >
    > Thanks
    > Nick


    No. Keep the files separate.
     
    Jason Heyes, Nov 16, 2004
    #6
  7. Victor Bazarov wrote:
    > Nick Forrington wrote:
    >
    >> Victor Bazarov wrote:
    >>
    >>> Nick Forrington wrote:
    >>>
    >>>> Hi, was wondering if anyone could help me out here, either by
    >>>> someone telling me how to do what I'm trying to do or by letting me
    >>>> know it can't be done...
    >>>>
    >>>> I've got a Game class and an Entity class, the Game has a number of
    >>>> entities in it, and I want the entity to have a reference to the
    >>>> game class.
    >>>>
    >>>> The compiler obviously doens't like this. I read someone where about
    >>>> a solution of just declaring the Game class "class Game;" without
    >>>> including the file, or vice versa, but this just makes the compiler
    >>>> complain when I try to access a method of the Game class.
    >>>
    >>>
    >>>
    >>>
    >>> Don't access methods until you define Game class completely.
    >>>
    >>>> Any ideas? can this even be done?
    >>>
    >>>
    >>>
    >>>
    >>> Sure. Put the member function definitions after both classes have been
    >>> fully defined. Get out of Java habit of defining all member functions
    >>> in the class definition.
    >>>

    > ------------------- this would be your [Base]Entity.h
    >
    >>> class Game;
    >>>
    >>> class Entity {
    >>> Game* pgame;
    >>> public:
    >>> void useGame();
    >>> };

    >
    > ------------------- this would be your Game.h
    > #include <Entity.h>
    >
    >>>
    >>> class Game {
    >>> std::list<Entity> entitylist;
    >>> public:
    >>> void move();
    >>> void doSomeEntityStuff(Entity* pe);
    >>> };

    >
    > ------------------- this would be your [Base]Entity.cpp
    > #include <Entity.h>
    > #include <Game.h>
    >
    >>>
    >>> void Entity::useGame() {
    >>> pgame->doSomeEntityStuff(this);
    >>> }
    >>>

    > ------------------- this would be your Game.cpp
    > #include <Game.h>
    > #include <Entity.h>
    >
    >>> void Game::move() { // blahblah
    >>> }

    >
    > ---------------------------------------------------
    >
    >>>
    >>>
    >>> V

    >>
    >>
    >>
    >> Thanks for the reply V
    >>
    >> At the minute I've got a Game.h, Game.cpp, BaseEntity.h and
    >> BaseEntity.cpp files, would I need to arrange it all so they're in the
    >> same file then?

    >
    >
    > No.
    >
    >> I'm using eclipse with the CDT plugin, which might be bad for doing
    >> this kind of thing.

    >
    >
    > I don't know what 'eclipse' or 'CDT plugin' are, sorry.
    >
    > V

    It works!

    Thanks a lot for your help, I didn't realise how simple it was. The
    problem I had before was not #including from the .cpp files - I kinda
    thought that wasn't allowed. It seems to be working now though.

    Thanks again, that's a big help.
    Nick
     
    Nick Forrington, Nov 17, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis Lerche

    Cyclic Dependency problem

    Dennis Lerche, May 11, 2004, in forum: C++
    Replies:
    6
    Views:
    11,257
    Khaled.Jouda
    Jul 5, 2010
  2. Replies:
    3
    Views:
    644
    Michael Mair
    Apr 22, 2006
  3. Replies:
    4
    Views:
    443
    Sakis
    May 17, 2006
  4. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    1
    Views:
    529
  5. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    3
    Views:
    403
    Michael
    Apr 13, 2007
Loading...

Share This Page