Sign in to follow this  

Having two classes both access each other

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem here. I have two classes: Character and Enemy with TakeDamage() functions. Now TakeDamage() takes the parameter of the attacker. So Character.cpp
void Character::TakeDamage(Enemy* &theEnemy)
{
	//code
}

Enemy.cpp
void Enemy::TakeDamage(Character* &theCharacter)
{
	//code
}

But my Library.h has this
//other header files
#include "Character.h"
#include "Enemy.h"
//other header files

"Enemy.h" has access to "Character.h" but "Character.h" doesn't have access to "Enemy.h" since the Enemy header file comes first. Is there a way to have both classes access each other?

Share this post


Link to post
Share on other sites
Certainly,

It's called a forward declaration. Putting something along the lines of


class Enemy;


in "Character.h" will most likely solve the issue. I must warn you though that circular dependencies are generally considered to be 'bad practice'. The reasoning behind this, amongst other things, is that dependencies between classes should be minimized to improve maintainability.

Cheers,
Tristan

Share this post


Link to post
Share on other sites
Not sure how you have your application set up but perhaps in your header file you could pre-declare your enemy/character class, then in your cpp file you could include the proper header file

Example:

Character.h

class Enemy; //pre-declare

class Character
{
public:
...
void TakeDamage(Enemy* &theEnemy);
...
};




Character.cpp

#include "Enemy.h"
...
void Character::TakeDamage(Enemy* &theEnemy)
...




Same for the enemy class

Enemy.h

class Character; //pre declare

class Enemy
{
public:
...
void TakeDamage(Character* &theCharacter);
...
};




Enemy.cpp

#include "Character.h"
...
void Enemy::TakeDamage(Character* &theCharacter);
...




I'm sure there are better ways to go about this, such as using some type of event system or object messaging system. However this method should work for an easy quick fix.

Hope that helps!

[edit] Sorry for the "double advice" Seems Tristan beat me to the punch![/edit]

Share this post


Link to post
Share on other sites
Something to remember about forward declarations they will make it so that you can have a pointer you can't have a regular instance variable. Something odd I noticed about your code, why are you using a reference to a pointer?

Share this post


Link to post
Share on other sites
If they are using a similar function, couldn't you have one class inherit from another? Isn't an enemy just a character that can't be controlled, and minus a few other things?

Instead of sending the whole enemy/character to the function, maybe only send the information that is actually required (attack stats).

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this