| | |
How to adjust class objects through seperate class functions???
![]() |
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...
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!!!
Now here are my classes...
C++ Syntax (Toggle Plain Text)
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!!!
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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:
Voila. No pointers required. And much more dynamic as well.
C++ Syntax (Toggle Plain Text)
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.
Last edited by skatamatic; Jan 11th, 2009 at 5:10 pm.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
•
•
•
•
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
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.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
C++ Syntax (Toggle Plain Text)
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: casting
- Next Thread: how to count the char you input..
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dissertation dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets





