Public Group

# C++ class problem

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

## Recommended Posts

I have a lifeform class that is a base class for the player and monster classes. I have a battle class in which I store the player and monsters who are in the battle. I want to make an attack function in the lifeform class, but it needs to know about the player and monsters in the battle. I cannot pass them as parameters though because it doesn't know what Monster and Player are. I can't include monster.h and player.h in the parent class. The only option I can think of is make the attack function in the battle class, unless there is something I am not thinking of which could very well be so.

##### Share on other sites
If you have the function take pointers or references to the Monster and Player classes, you can use forward declarations. Ex:
class Player;class Monster;class LifeForm {  public:    void Fight(Player &, Monster &);};

On the other hand, I don't understand why you need to have the attack function in the life form class take both a player and a monster. Wouldn't it just need a reference to the life form it's attacking?

##### Share on other sites
What you can do is this:

//base.hclass CPlayer;class CMonster;class CBase {  void MyFunc(CPlayer* Player);  void MyFunc(CMonster* Monster);};//base.cpp#include "base.h"#include "player.h"#include "monster.h"

That way the base class can know about the derived classes. There is probably a better way of designing the system than that but I can't say what from the small amount of info you presented, but the code above will work.

##### Share on other sites
Quote:
 but it needs to know about the player and monsters in the battle.

Why does it need to know? Something doesn't sound right.

##### Share on other sites
I thought something didn't seem right also that is why I am posted here. My battle function stores a vector of monsters and a player for that battle. In the attack function the attacking lifeform searches for other lifeforms around it that are against it player vs monsters, monster vs player. The target is chosen and the lifeform attacks. I then get information about the player or monster such as hit chance, dodge chance and then if the lifeform hits I call a take Dmg function on whatever is recieving the dmg.

So in searching for targets, and getting information about the target I need to know about them.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633755
• Total Posts
3013706
×