Mutator

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

// 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
		virtual int 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

Then decide whether you're getting or setting.
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()


Updated code on the line that had error:
1
2
newDamage = Creature2.getHitpoints() - damage;
	cout << newDamage << endl;



1
2
3
4
5
6
7
8
9
10
11
12
13
// getter
int Creature::getHitpoints() const 
	{
		return hitpoints;
	}


//setter
void Creature::setHitpoints(int newHitpoints) 
	{
		this-> hitpoints = newHitpoints;
		return;
	}

I don't think my solution
1
2
 newDamage = Creature2.getHitpoints() - damage;
	cout << newDamage << endl; 
is the correct way to go about it though since the actual data member needs to be modified.

Here is what is described in the instructions:

When I say "subtract that amount from Creature2's hitpoints, I mean that the actual hitpoints data member of the Creature2 object will be modified.
It's going to be something like
1
2
3
damage = Creature1.getDamage();
hits = Creature2.getHitpoints();
Creature2.setHitpoints(hits-damage);



Though perhaps consider
1
2
3
4
5
6
7
void Creature::updateDamage(int damage) 
{
    this->hitpoints -= damage;
    if ( this->hitpoints < 0 ) {
        // snuff it
    }
}

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 like the function idea too.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

damage = Creature1.getDamage();
	cout << " Total damage = " << damage << endl;
	
	hits = Creature2.getHitpoints();
	Creature2.setHitpoints(hits - damage);
	if (Creature2.getHitpoints() < 0) 
		cout << "Creature1 is dead!" << endl;

	else cout << "Creature1 is not dead yet!" << endl;


	damage = Creature2.getDamage();
	cout << " Total damage = " << damage << endl;
	
	hits = Creature1.getHitpoints();
	Creature2.setHitpoints(hits - damage);
	if (Creature1.getHitpoints() < 0)
		cout << "Creature2 dead!" << endl;

	else cout << "Creature2 is not dead yet!" << endl;
1
2
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.


Also, you're copy/pasting code.

1
2
3
4
5
6
7
8
void fight( Creature &attacker, Creature &defender) {
    damage = attacker.getDamage();
    cout << " Total damage = " << damage << endl;
    hits = defender.getHitpoints();
    defender.setHitpoints(hits - damage);
    if (defender.getHitpoints() < 0) 
        cout << defender.getName() << "is dead!" << endl;
}


Which you then call from your next higher level function
1
2
fight(Creature1,Creature2);
fight(Creature2,Creature1);


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.
The fight does not seem to affect the attacker. If so, then make it explicit:
void fight( const Creature &attacker, Creature &defender);

Is getHitpoints() == 0 dead or alive?

Should a creature have negative hit points?
(Perhaps, a Resurrect spell could vary based on how dead a creature is.)
Last edited on
I was able to complete the project, and I appreciate the help.

Thank you!
Topic archived. No new replies allowed.