I have some classes set up based on inheritance and I am trying access a mutator function setHitpoints() in the base class from the client program. I am displaying the damage from creature1 correctly and now I need to subtract that damage from creature2 by modifying its data member. The data member is private and I am trying to use the mutator function to handle this but I get an error that says:
error C3867: 'cs_creature::Creature::setHitpoints': non-standard syntax; use '&' to create a pointer to member
// in main
Elf e(50, 50);
Balrog b(50, 50);
battleArena(e, b);
}
void battleArena(Creature& Creature1, Creature& Creature2)
{
int damage;
int newDamage;
damage = Creature1.getDamage();
cout << " Total damage = " << damage << endl;
newDamage = Creature2.setHitpoints - damage; // error here
cout << newDamage;
}
class Creature
{
private:
int strength;
int hitpoints;
public:
Creature();
Creature(int newStrength, int newHitpoints);
virtual string getSpecies() const = 0; // returns the type of the species
virtualint getDamage() const; // returns the amount of damage this Creature
// also include appropriate accessors and mutators
int getStrength() const;
int getHitpoints() const;
void setStrength(int newStrength);
void setHitpoints(int newHit);
};
void Creature::setStrength(int newStrength)
{
this->strength = newStrength;
return;
}
> newDamage = Creature2.setHitpoints - damage; // error here
At the very least, you need to make it a function call with some () newDamage = Creature2.setHitpoints() - damage; // error here
Good point on the brackets. I think I got it to work by calling the getHitpoints function and subtracting the damage. Shouldn't I be able to use the setHitpoints function though? I'm not getting that part right.
Note: I accidentally put a setStrength() above instead of setHitpoints()
I tried to make a call to setHitpoints similar to what you did, but I cant't cout it to check on my value. I think the << would need to be overloaded. Is there a way to check the value of stHitpoints after doing this?
I can't seem to directly check on the updated member (with some kind of cout) variable hitpoints because it's private, but it seems to be working correctly with this code I set up.
if (Creature1.getHitpoints() < 0)
cout << "Creature2 dead!" << endl;
There's no point checking Creature1 if you're trying to check Creature2 is dead.
It's not helped by having meaningless names like Creature1 and Creature2.
Note that you presently favour Creature1.
Do you want the possibility that two otherwise closely matched opponents can both simultaneously land a killer blow?
I threw that up quick to just to see how it's working. I am currently changing things and modifying it by calling the getSpecies() function to represent who's doing an attack and who gets killed. Now I am seeing creature1 is showing not dead when he should be dead. I think that's what you mean by favoring creature1.