×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Mutual Inclusion

Mutual Inclusion

Mutual Inclusion

(OP)
I realise this seems a stupid question but I have googled this to death with no results so here goes:

I have two files, game.h and player.h. Each contains a reference to the other.

I started by simply putting an include to the other each file. This resulted in an infinite loop when I compiled the program, fair enough really!

So I tried the following, which I found in a c++ forum.

player.h

#ifndef PLAYER_H
#define PLAYER_H

class Player() {
...
thisIsAFunction( Game&; );
...
}

#endif


game.h

#ifndef GAME_H
#define GAME_H

class Game() {
...
}

#endif


The problem I have now is that the compiler reports an error on the thisIsAFunction( Game&; ); line. 'Game' has not been declared.

I should mention that the main.cpp file includes game.h, the game class (an instance of which is created by the main() function) has a member of type Player and the method in the Player class requires a Game object to be passed to it (otherwise I wont be able to draw to the screen!!)

Does anyone know any way around my problem? If I cant do this mutual inclusion thing it could make my code almost unmanageably longer!!!

I am using the gcc complier (i think, not sure how to check!) and using Code::Blocks IDE.

HELP!!!!!!!

RE: Mutual Inclusion

Try

CODE

#ifndef PLAYER_H
#define PLAYER_H

class Game;
class Player() {
...
thisIsAFunction( Game&; );
...
}

#endif
It's a forward reference, which tells the compiler that Game will be properly declared later.  But for now, it only knows that the class exists.  So you can't create instances of a Game, but you can point to a Game.

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close