Cyclic dependency

N

Nick Forrington

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
 
V

Victor Bazarov

Nick said:
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
 
R

Ron Natalie

Nick said:
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();
}
 
N

Nick Forrington

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



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
 
V

Victor Bazarov

Nick said:
Victor said:
Nick said:
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
------------------- this would be your [Base]Entity.cpp
------------------- this would be your Game.cpp
#include said:
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
 
J

Jason Heyes

Nick Forrington said:
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.
 
N

Nick Forrington

Victor said:
------------------- this would be your [Base]Entity.h

------------------- this would be your Game.h

------------------- this would be your [Base]Entity.cpp
#include <Entity.h>
------------------- this would be your Game.cpp
#include <Game.h>
#include said:
---------------------------------------------------




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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top