Classes that need to know each other..

Started by
11 comments, last by Bacterius 11 years, 1 month ago

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.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

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

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

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?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement