I want to create a battle system for an RPG game, one a little more robust then the previous one I made. This one I want to be very dynamic and easy to use. I want to use the Allegro API for it, but first I want to make sure I have everything going good in console before I put it into a real time rpg, but I'm having technical difficulties...

Now here are my classes...

class Game{
};

class Boxer:Game{
    public:
        char name[256];
        int health, *attack, level, critchance,xp,requiredxp;
        int Attack(){return -(*attack-rand()%(level/2));}
        void setName(){}
        void AdjustXP(int x){xp+=x;}
        void     Spash(){printf("HP:%d\tXP:%d/%d\n",health,xp,requiredxp);}
        void EveryFrame(){*attack=level*3;}
        Boxer(){health=25;level=15;requiredxp=level*5,xp=0;}
};



class Battle:Game{
    public:
        void Attack(int  attackerhit, int * attackeehp){*attackeehp+=attackerhit;}
};

int main(){
srand(time(NULL));
Boxer Boxer; 
Battle Battle;
int attack=5;
Battle.Attack(attack,Boxer->hp) //??? Heres my problem I believe
cout<<Boxer.hp;
}

Right now I want to pass the Boxer class' HP through a pointer to the Battle Classes Attack function, while passing a normal int (since the attack really doesnt need to be adjusted as of now). I get really weird errors like no matching function for call to `Battle::Battle(int&, int)'|... Please help!!!

Recommended Answers

All 7 Replies

Well I can see a few issues with this code. First of all, when someone gets hit, i assume they would lose hp, not gain it. And as to the actual problem, the Boxer class doesn't contain a member called hp. The best way to do something like this is to make all the members in your classes private, then make some public accessor methods like this:

class Boxer:Game{
    private:
        char name[256];
        int health, *attack, level, critchance,xp,requiredxp;
    public:
        int Attack(){return -(*attack-rand()%(level/2));}
        void setName(){}
        void AdjustXP(int x){xp+=x;}
        void     Spash(){printf("HP:%d\tXP:%d/%d\n",health,xp,requiredxp);}
        void EveryFrame(){*attack=level*3;}
        int GetHp() const
        {
               return health;
        }
        void SetHp(int const & hp)
        {
               health = hp;
        }
        Boxer(){health=25;level=15;requiredxp=level*5,xp=0;}
};

class Battle:Game{
    public:
        void Attack(int const & attackerhit, Boxer & box){box.SetHp(box.GetHp() - attackerhit);}
};

Voila. No pointers required. And much more dynamic as well.

This might not seem dynamic, since a boxer is required as an argument rather than an arbitrary pointer, but I'm making the assumption that if you would like to include more types of boxers/etc that you would derive these classes from the basic boxer class, in which case you could make GetHP() and SetHP() polymorhpic (ie virtual methods). But you don't seem to be at this stage yet, so this should suffice.

so far ty, however i have 2 questions, I dont know why I need a const in there, and I cant find any really helpful tuts on polymorphism and class deriving, can you lead me to a good one :P

so far ty, however i have 2 questions, I dont know why I need a const in there, and I cant find any really helpful tuts on polymorphism and class deriving, can you lead me to a good one :P

1. Everything that you don't want to modify in a function should have a const tag. And for the function itself, if you don't want to modify any of the classe's members inside a function, it is good practice to append a const modifier to the end of the function (as demonstrated with with GetHp).

2. I can't think of any good tutorials off the top of my head. Your best bet would be to take an intermediate c++ course. Or google polymorphism and base class inheritance.

commented: Excellent postings! +1

ugh can you just show me what you would do in the int main() too I'm really having severe problems, me and pointers are just not friends

int main(){
srand(time(NULL));
Boxer Boxer; 
Battle Battle;
Battle.(Boxer.Attack(),Boxer) //solution
cout<<Boxer.GetHp();
}

This would work. But your code is fundamentally sloppy. Are you trying to teach c++ to yourself? I wouldn't recommend that, since c++ is the most complicated programming language in the world, in my opinion (next to Lisp). Try C# or VisualBasic. Or take a course on c++. Or if you're really addemant about teaching yourself, prepare for a long and frustrating battle.

Yes, I've been teaching myself for awhile. I'm fine with most aspects, its just pointers have always kicked my ass in one way or another, like the compiler will expect me to put something in that pointing to the pointer of something (**) and I'll google, not figure it out, then just post here. This is due to the fact that you guys rawk!!! thx much skata :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.