Original Post
Hi all,
Having a problem with my coding...
I'm trying to get classes to talk to each other freely. For example my player class must be able to speak to my weapon class. Weapon has to be able to talk BACK to player class to check for ordnance collisions and stuff (FYI haven't got a clue how to do that yet, but paving the way!).
Now I've tried setting up pointers for weapon class inside the player class OR vise versa... which is fine, this works. However as soon as I try to set up a pointer in BOTH classes to talk to each other it won't compile.
I've tracked this problem down to the fact that I can't have the following due to recursive includes (is that what it's called?)
[source lang="cpp"]
// Filename: playerclass.h.
#include "weaponclass.h"
class PlayerClass
{
//blah blah blah
private:
WeaponClass* weapon;
};[/source]
[source lang="cpp"]
// Filename: weaponclass.h.
#include "playerclass.h"
class WeaponClass
{
//blah blah blah
private:
PlayerClass* player;
};[/source]
Is there way to do this without the compiling errors?
I have though of have a class that works as a communicator with the others. Maybe implementing this into the engineclass, but I wasn't sure if this is classed as good practice...
Any help would be greatly appreciated...
Mike
Having a problem with my coding...
I'm trying to get classes to talk to each other freely. For example my player class must be able to speak to my weapon class. Weapon has to be able to talk BACK to player class to check for ordnance collisions and stuff (FYI haven't got a clue how to do that yet, but paving the way!).
Now I've tried setting up pointers for weapon class inside the player class OR vise versa... which is fine, this works. However as soon as I try to set up a pointer in BOTH classes to talk to each other it won't compile.
I've tracked this problem down to the fact that I can't have the following due to recursive includes (is that what it's called?)
[source lang="cpp"]
// Filename: playerclass.h.
#include "weaponclass.h"
class PlayerClass
{
//blah blah blah
private:
WeaponClass* weapon;
};[/source]
[source lang="cpp"]
// Filename: weaponclass.h.
#include "playerclass.h"
class WeaponClass
{
//blah blah blah
private:
PlayerClass* player;
};[/source]
Is there way to do this without the compiling errors?
I have though of have a class that works as a communicator with the others. Maybe implementing this into the engineclass, but I wasn't sure if this is classed as good practice...
Any help would be greatly appreciated...
Mike