Jump to content

  • Log In with Google      Sign In   
  • Create Account

Classes that need to know each other..


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 01 March 2013 - 09:17 AM

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



Sponsor:

#2 Andy474   Members   -  Reputation: 685

Like
0Likes
Like

Posted 01 March 2013 - 09:23 AM

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 smile.png

 

-edit: all the bad types and code tags


Edited by Andy474, 01 March 2013 - 09:27 AM.


#3 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 01 March 2013 - 09:28 AM

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 smile.png

 

-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.



#4 Andy474   Members   -  Reputation: 685

Like
0Likes
Like

Posted 01 March 2013 - 09:31 AM

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.

If your directory looks like

-Classes

   - CEntity.h

   - GUI.

      - CHealthBar

 

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


Edited by Andy474, 01 March 2013 - 09:38 AM.


#5 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 01 March 2013 - 09:37 AM

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'



#6 Nercury   Crossbones+   -  Reputation: 766

Like
0Likes
Like

Posted 01 March 2013 - 09:42 AM

If you predefine 

class CHealthBar;

 

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



#7 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 01 March 2013 - 09:45 AM

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



#8 Serapth   Crossbones+   -  Reputation: 5475

Like
1Likes
Like

Posted 01 March 2013 - 09:46 AM

CHealthBar HealthBar;

 

That is your problem.

 

 

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, 01 March 2013 - 09:48 AM.


#9 Andy474   Members   -  Reputation: 685

Like
0Likes
Like

Posted 01 March 2013 - 09:49 AM

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

 

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

 

Edit - Serapth got in first :)


Edited by Andy474, 01 March 2013 - 09:51 AM.


#10 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 01 March 2013 - 09:49 AM

CHealthBar HealthBar;

 

That is your problem.

 

 

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, 01 March 2013 - 10:03 AM.


#11 EddieV223   Members   -  Reputation: 1406

Like
0Likes
Like

Posted 01 March 2013 - 07:42 PM

CHealthBar HealthBar;

 

That is your problem.

 

 

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.


If this post or signature was helpful and/or constructive please give rep.

 

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

 

// Easy to learn 2D Game Library c++

SFML2.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

 


#12 Khatharr   Crossbones+   -  Reputation: 3003

Like
0Likes
Like

Posted 01 March 2013 - 08:29 PM

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.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#13 Bacterius   Crossbones+   -  Reputation: 8885

Like
1Likes
Like

Posted 02 March 2013 - 12:28 AM

Usually having these kinds of circular references in an indication of dubious design. I don't know, though. It happened a lot when I started programming and now I always avoid it. Try refactoring with a different architecture, maybe?


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS