Jump to content
  • Advertisement
Sign in to follow this  
Grahf750

C++ class problem

This topic is 4830 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 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 this post


Link to post
Share on other sites
Advertisement
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?

edit: More information on forward declarations and other good stuff.

Share this post


Link to post
Share on other sites
What you can do is this:


//base.h

class 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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!