# Classes that need to know each other..

This topic is 1784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, so since I am a noob I found myself stuck on a small problem. And the problem is that I have a Entity class that contains a Healthbar instance and I need the healthbar to contain a pointer to its owner (the entity). And this is where I got stuck.

CEntity class:

#ifndef _CENTITY_H_
#define _CENTITY_H_

#include "SDL.h"

class CEntity
{

public:
CEntity();
~CEntity();

public:
bool OnInit();
void OnRender(SDL_Surface* destination);
void OnLoop();

int GetX();
int GetY();

int GetW();
int GetH();

int GetMaxHealth();
int GetCurrentHealth();

private:
SDL_Rect Image;

CHealthBar HealthBar;
int MaxHealth;
int CurrentHealth;

int LastHurtTime;

};

#endif


CHealthBar class:

#ifndef _CHEALTHBAR_H_
#define _CHEALTHBAR_H_

#include "SDL.h"

class CHealthBar
{

public:
CHealthBar();
~CHealthBar();

public:
bool OnInit();
void OnRender(SDL_Surface* _destination);
void OnLoop();

void SetOwner(CEntity* _owner);

private:
CEntity* Owner;

SDL_Rect BarBackground;
SDL_Rect Bar;

int MaxBarLenght;

SDL_Color HighHealth;
SDL_Color AverageHealth;
SDL_Color LowHealth;

};

#endif


The compiler complains about this to no end, what should I do about this? Is this not a good way to handle the way a healthbar is connected to an entity? I've tried to include them here and there to no success, even tried to forward declare and still not getting it to work.

Thanks

##### Share on other sites

Don't you need to #include the files?

At the top of "CHealthBar.h" do #include "CEntity.h"  to tell the compiler you are using the class defined in the file, else it wont know what CEntity is and likewise in the other file.

You may also need to pre-define the classes, so at the top of CHealthBar ... I vaguly remeber having toto this in my last game.

#ifndef _CHEALTHBAR_H_
#define _CHEALTHBAR_H_

#include "SDL.h"
#inlcude "CEntity.h";

class CEntity;

class CHealthBar
{ ...


And likewise in the other file, hope this help

-edit: all the bad types and code tags

Edited by Andy474

##### Share on other sites

Don't you need to #include the files?

At the top of "CHealthBar.h" do #include "CEntity.h"  to tell the compiler you are using the class defined in the file, else it wont know what CEntity is and likewise in the other file.

You may also need to pre-define the classes, so at the top of CHealthBar ... I vaguly remeber having toto this in my last game.

#ifndef _CHEALTHBAR_H_
#define _CHEALTHBAR_H_

#include "SDL.h"
#inlcude "CEntity.h";

class CEntity;

class CHealthBar
{ ...


And likewise in the other file, hope this help

-edit: all the bad types and code tags

Well I did not have that in this example but I have tried that and tried it again now and it still gives me errors like "uses undefined class CHealthBar" etc.

##### Share on other sites

ok, have you predefined the class in each file? so as aove in CEntity you have put class CHealthBar, and can you post the Compiler Errros, might make it easier for other to diagnose.

also, iirc they have to be in the same directory level.

E.g.

-Classes

- CEntity.h

- GUI.

- CHealthBar

then the include in CENtity must be #include "GUI\CHealthBar"

Edited by Andy474

##### Share on other sites

ok, have you predefined the class in each file? so as aove in CEntity you have put class CHealthBar, and can you post the Compiler Errros, might make it easier for other to diagnose.

#include "CHealthBar.h"

class CHealthBar;


this is in my CEntity.h file.

and this is in my CHealthBar file:

#include "CEntity.h"

class CEntity;


and when i compile i get this error:

error C2079: 'CEntity::HealthBar' uses undefined class 'CHealthBar'

##### Share on other sites

If you predefine

class CHealthBar;


you do not need to #include it. You #include it in .cpp file.

##### Share on other sites

If you predefine

class CHealthBar;


you do not need to #include it. You #include it in .cpp file.

Yes and when I do that I get these errors:

error C2079: 'CEntity::HealthBar' uses undefined class 'CHealthBar'

error C2228: left of '.SetOwner' must have class/struct/union

error C2228: left of '.OnRender' must have class/struct/union

error C2228: left of '.OnLoop' must have class/struct/union

##### Share on other sites

CHealthBar HealthBar;

When using a forward declaration, you can only use a POINTER to the object, you cant actually declare an instance, as the compiler has no idea of the actual composition and size of a forward declared class.

Change CHealthBar to a reference or pointer, and your code should work.

EDIT: This StackOverflow answer explains it in a bit more detail.  So either change it so CEntity is aware of CHealthBar, or if you are going to continue using a forward declaration, make CHealthBar in CEntity a pointer or reference.

Edited by Serapth

##### Share on other sites

hmm, I have run these two files (using VS2012)  and have no problems.

mb cus i've got no CPP files but also i havea  feeling you need todo CHealthBar* HealthBar;

Edit - Serapth got in first :)

Edited by Andy474

##### Share on other sites

CHealthBar HealthBar;

When using a forward declaration, you can only use a POINTER to the object, you cant actually declare an instance, as the compiler has no idea of the actual composition and size of a forward declared class.

Change CHealthBar to a reference or pointer, and your code should work.

EDIT: This StackOverflow answer explains it in a bit more detail.  So either change it so CEntity is aware of CHealthBar, or if you are going to continue using a forward declaration, make CHealthBar in CEntity a pointer or reference.

Ok, thank you. Will try to get that up and running then.

Yep got it working, thank you :)

Edited by Nausea

##### Share on other sites

CHealthBar HealthBar;

When using a forward declaration, you can only use a POINTER to the object, you cant actually declare an instance, as the compiler has no idea of the actual composition and size of a forward declared class.

Change CHealthBar to a reference or pointer, and your code should work.

EDIT: This StackOverflow answer explains it in a bit more detail.  So either change it so CEntity is aware of CHealthBar, or if you are going to continue using a forward declaration, make CHealthBar in CEntity a pointer or reference.

Pointer or reference as long as you don't dereference them.  Its also worth noting that you can use a smart pointer so you don't have to use naked pointers.

##### Share on other sites
As a side note, should Entity really have a health bar? I would think that something like 'Actor' would derive from Entity and Actor would have a health bar.

I dunno your layout though. Just a though.