473,411 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Cyclic dependency

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
Jul 22 '05 #1
6 1484
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
Jul 22 '05 #2
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
Jul 22 '05 #3
"Nick Forrington" <n.**********@warwick.ac.uk> wrote in message
news:cn**********@wisteria.csv.warwick.ac.uk...
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.
Jul 22 '05 #4
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
Jul 22 '05 #5
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
Jul 22 '05 #6
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-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();
}
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Brian Sabolik | last post by:
I'm not sure if I've broken any Object Oriented rules or not, but ... I have projects in 2 different solutions that need to use each other's methods. Therefore I may have an "update" method in...
3
by: Dennis Lerche | last post by:
Hi I have a problem regarding cyclic dependency, yeahh I know bad design. But right at this moment I can't see how it should be redesigned to avoid this. The problem is that I just can't get it...
4
by: Alex Sedow | last post by:
For example, System.dll contains assembly reference to System.XML.dll, System.XML.dll refers to System.dll. Some another open projects contain cyclic assembly dependencies too. How C# compiler...
3
by: Pohihihi | last post by:
This is a report from VSS newsgroup in hope of getting some more suggestions. Thanks, Po ------------------------------------------- I guess I am not getting the whole picture here. How does...
3
by: fc2004 | last post by:
Hi, Is there any tools that could report where cyclic header dependency happens? this would be useful when working with a large project where tens or hundreds of headers files may form complex...
4
by: sakis.panou | last post by:
Hi all, Can anyone explain to me why the copy constructor of the COuterClass is getting called for this one? Let me start by saying I reckon this is seriously bad way of implementing anything of...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
1
by: pallav | last post by:
I have to header files, circuit.h and latch.h that reference each other and are causing a cyclic dependency. latch.h file #include "circuit.h" typedef boost::shared_ptr<struct LatchLatchPtr;...
3
by: soup007 | last post by:
Hi, I am having some difficulties with cyclic dependency between two classes. Situation is something like following - ///A.h #include "B.h" class A { { int X; public:
11
by: sgurukrupagmailcom | last post by:
Hi, When searching for a solution to my problem I stumbled upon 'Curiously Recurring Template Pattern' see link. This is how the pattern looks: template < typename T > struct y { } ;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.