Connecting Tech Pros Worldwide Forums | Help | Site Map

Cyclic dependency

Nick Forrington
Guest
 
Posts: n/a
#1: Jul 22 '05
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

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Cyclic dependency


Nick Forrington wrote:[color=blue]
> 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.[/color]

Don't access methods until you define Game class completely.
[color=blue]
> Any ideas? can this even be done?[/color]

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
Nick Forrington
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Cyclic dependency


Victor Bazarov wrote:[color=blue]
> Nick Forrington wrote:
>[color=green]
>> 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.[/color]
>
>
> Don't access methods until you define Game class completely.
>[color=green]
>> Any ideas? can this even be done?[/color]
>
>
> 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[/color]

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
Jason Heyes
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Cyclic dependency


"Nick Forrington" <n.forrington@warwick.ac.uk> wrote in message
news:cne0v8$nam$1@wisteria.csv.warwick.ac.uk...[color=blue]
> 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[/color]

No. Keep the files separate.


Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Cyclic dependency


Nick Forrington wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>> Nick Forrington wrote:
>>[color=darkred]
>>> 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.[/color]
>>
>>
>>
>> Don't access methods until you define Game class completely.
>>[color=darkred]
>>> Any ideas? can this even be done?[/color]
>>
>>
>>
>> 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.
>>[/color][/color]
------------------- this would be your [Base]Entity.h[color=blue][color=green]
>> class Game;
>>
>> class Entity {
>> Game* pgame;
>> public:
>> void useGame();
>> };[/color][/color]
------------------- this would be your Game.h
#include <Entity.h>[color=blue][color=green]
>>
>> class Game {
>> std::list<Entity> entitylist;
>> public:
>> void move();
>> void doSomeEntityStuff(Entity* pe);
>> };[/color][/color]
------------------- this would be your [Base]Entity.cpp
#include <Entity.h>
#include <Game.h>[color=blue][color=green]
>>
>> void Entity::useGame() {
>> pgame->doSomeEntityStuff(this);
>> }
>>[/color][/color]
------------------- this would be your Game.cpp
#include <Game.h>
#include <Entity.h>[color=blue][color=green]
>> void Game::move() { // blahblah
>> }[/color][/color]
---------------------------------------------------[color=blue][color=green]
>>
>>
>> V[/color]
>
>
> 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?[/color]

No.
[color=blue]
> I'm using eclipse with the CDT plugin, which might be bad for doing this
> kind of thing.[/color]

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

V
Nick Forrington
Guest
 
Posts: n/a
#6: Jul 22 '05

re: Cyclic dependency


Victor Bazarov wrote:[color=blue]
> Nick Forrington wrote:
>[color=green]
>> Victor Bazarov wrote:
>>[color=darkred]
>>> 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.
>>>[/color][/color]
> ------------------- this would be your [Base]Entity.h
>[color=green][color=darkred]
>>> class Game;
>>>
>>> class Entity {
>>> Game* pgame;
>>> public:
>>> void useGame();
>>> };[/color][/color]
>
> ------------------- this would be your Game.h
> #include <Entity.h>
>[color=green][color=darkred]
>>>
>>> class Game {
>>> std::list<Entity> entitylist;
>>> public:
>>> void move();
>>> void doSomeEntityStuff(Entity* pe);
>>> };[/color][/color]
>
> ------------------- this would be your [Base]Entity.cpp
> #include <Entity.h>
> #include <Game.h>
>[color=green][color=darkred]
>>>
>>> void Entity::useGame() {
>>> pgame->doSomeEntityStuff(this);
>>> }
>>>[/color][/color]
> ------------------- this would be your Game.cpp
> #include <Game.h>
> #include <Entity.h>
>[color=green][color=darkred]
>>> void Game::move() { // blahblah
>>> }[/color][/color]
>
> ---------------------------------------------------
>[color=green][color=darkred]
>>>
>>>
>>> V[/color]
>>
>>
>>
>> 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?[/color]
>
>
> No.
>[color=green]
>> I'm using eclipse with the CDT plugin, which might be bad for doing
>> this kind of thing.[/color]
>
>
> I don't know what 'eclipse' or 'CDT plugin' are, sorry.
>
> V[/color]
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
Ron Natalie
Guest
 
Posts: n/a
#7: Jul 22 '05

re: Cyclic dependency


Nick Forrington wrote:[color=blue]
> 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.
>[/color]
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-lit...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();
}
Closed Thread