Archived

This topic is now archived and is closed to further replies.

BlackEye

3's a crowd!

Recommended Posts

Hi guys, I have a base class object, and 2 classes ball and player which inherit from this class. Now i need my player class to have a pointer to the ball in order to influence it by calling its object class functions(physics). However i would also like my ball to see the player class? But i cannot set this up in the #includes as they call each other before either has been declared!? Understand? Example.. Player class
//[Player .h]

...
...
#include "cball.h"
...
...
Ball class
//[Ball.h]

...
...
#include "cPlayer.h"
...
...

Is there a solution to this problem? Thanks again guys Pete.

Share this post


Link to post
Share on other sites
Err I know is pointers!Thats a given!
But the #includes are the problem, when making my player class its told to include the ball class so i can have a pointer to a ball, so it reads the ball class. However the ball class has #include "player.h" as it needs the player class(As it has a pointer to a player!).

So it gets stuck

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
forward declaration, search for it on google.

Basicly,
declare an empty class cPlayer in cball.h
and an empty class cBall in cplayer.h
then include cball.h and cplayer.h in cpp files.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you need to do forward declarations of the classes.
if your classes are called ball and player then do something like:

// Player.h
class player;

#include "ball.h"

class player{
...
ball* m_ball; //ok
};


---<snip>---

//Ball.h
class ball;

// all includes AFTER the forward declaration
#include "player.h"

class ball{
...
player* m_player; //ok
};

Share this post


Link to post
Share on other sites

I believe his main problem is multiple inclusion. That''s what is messing up his design. BlackEye, your ideas were correct from the beginning, forward declaration is only neccecary when you want two classes in the SAME header to share pointers. When they are in seperate files, this is no longer an issue.

Simply do like this, in each header, before the includes, before EVERYTHING include these lines


#ifdef HEADER_MACRO_WHATEVER // can be the filename if you want

#define HEADER_MACRO_WHATEVER

// Here goes all header code, classes, structs, whatever


#endif


Put that in both your headers and it will work without forward declaration. Oh and don''t forget the endif in the end, heh.

Share this post


Link to post
Share on other sites
Thanks guys , the forward declarations got it.

The #ifdef HEADER_MACRO_WHATEVER etc seems to raise errors in the cpp files. Not to worry if the forward declarations start failing i will figure out why.

Thanks guys, your all great!



Share this post


Link to post
Share on other sites
ARG! I misspelled dammit! That''s why it raises errors, just please try instead of writing #ifdef as the first line, do #ifndef (see the letter ''n'' in there?)

Sorry, my mistake.

Also, the technique I presented fixes another error; multiple inclusion. If you put that in each header, each header can be included by virtually an infinite number of other headers. That''s quite a nifty thing.

TIP: If you are using any version of MSVC you can replace all of that with a simple
#pragma once
statement at the TOP of each header file. It is faster than the standard #ifndef statements, but it works only on MSVC compilers as far as I know.

Share this post


Link to post
Share on other sites