//Warrior Class, gives hp and change hp function
class Warrior
{
private:
int hp;
public:
Warrior(int x) : hp(x) {}
void changehp(int x){hp+=x;}
};
//Attack class has losehp function,
//which takes a warrior object as a parameter
//Errors here!
class Attack
{
public:
void losehp(Warrior& name, int x){name.changehp(x);}
};
//hpup, also takes a warrior object
//and works like a charm!
void hpup(Warrior& name, int x) {name.changehp(x);}
int main()
{
Warrior dirk(10);
hpup(dirk, 15); //Works just fine
Attack a1;
a1.losehp(dirk, -5);
//gives multiple errors:
// syntax error: identifier 'Warrior'
// void Attack""losehp(Warrior&, int):overloaded function not found
//Attack::losehp function does not take 2 arguments
How can I have a1 subtract 5 from the hp of dirk? How can I use a member function (losehp) from a different class (Attack) to change the hp of a different class' object(dirk)?
And why does it tell me that losehp does not take 2 arguments?
Line 7: Remove Warrior::
Line 8: Missing ; after hp+=x
Line 8: Extraneous ; after }
Line 24: Missing ; after name.changehp(x)
Extransous ; after }
Line 30: Warrior is instantied with two arguments, but only constructor accepts one argument.
Thanks for the help with the syntax, but I just wrote it, to give the idea. None of those syntax errors you mentioned has much to do with the issue I am trying to get at. I was actually just erasing code to make my problem focused and I guess I erased some of the code that should have stayed. I'll edit it, but the problem still stands as originally stated.
//Warrior Class, gives hp and change hp function
class Warrior
{
private:
int hp;
public:
Warrior(int x) : hp(x)
{}
void changehp(int x)
{ hp+=x; }
};
//Attack class has losehp function,
//which takes a warrior object as a parameter
class Attack
{
public:
void losehp(Warrior& name, int x)
{ name.changehp(x); }
};
//hpup, also takes a warrior object
//and works like a charm!
void hpup(Warrior& name, int x)
{ name.changehp(x); }
int main()
{ Warrior dirk(10);
hpup(dirk, 15); //Works just fine
Attack a1;
a1.losehp(dirk, -5);
return 0;
}
BTW, I find it contradictory that losehp adds to hp. One would expect losehp to decrement hp by the specified number of points.
Yeah, it does compile fine doesn't it. So I found the heart of my problem, and it seems like it stems from how the files link.
I have a header for attack and warrior, each with their own implementation file (I think that is the correct name). Then I have main.cpp.
When I try to run it with different headers, it gives me an error. But when I paste all the code into one file, into the main.cpp file, it runs cleanly.
It is strange that I only get compile errors when I try to pass an object of class Warrior to a1, but if I change the function so that it only accepts 1 int parameter it runs fine. Why would it say that a1.losehp does not accept two arguments and that overloaded function cannot be found in attack when I have losehp with a Warrior parameter? Thanks for the help.
When I try to run it with different headers, it gives me an error.
I assume you mean compile, not run.
You're going to have to be specific about the compile errors you're getting when you compile with separate headers. There has to be something about how you're including the headers. Your include of warrior.h needs to precede your include of attack.h. Other than that, I see nothing that should cause problems when putting the delcarations in separate files.