Reputation: 5
I am fairly new to c++. As a project I was rewriting a little game I wrote in python (I never got it to work correctly). During compilation I get this error: error: no match for ‘operator-=’
I know that this operator exists in c++, so why am i getting this error?
code:
void rpg() {
cout << "This mode is not yet complete. It only contains a dungeon so far. I'm still working on the rest!";
dgn();
}
void dgn() {
int whp = 100;
int mahp = 100;
int hhp = 100;
string m;
int mhp;
cout << "There are three passages. Do you take the first one, the second one, or the third one? (Give your answer in numbers)";
int psg;
cin >> psg;
switch (psg) {
case 1:
m = "Troll";
mhp = 80;
break;
case 2:
m = "Goblin";
mhp = 35;
break;
case 3:
m = "Dragon";
mhp = 120;
}
cout << "A ";
cout << m;
cout << " appears!";
dgnrd(m, mhp, whp, mahp, hhp);
}
void dgnrd(string m, string mhp, int whp, int mahp, int hhp) {
bool alive = true;
while (alive) {
string wa;
string ma;
string ha;
cout << "What does Warrior do? ";
cin >> wa;
cout << "What does Mage do? ";
cin >> ma;
cout << "What does Healer do? ";
cin >> ha;
if (wa == "flameslash") {
cout << "Warrior used Flame Slash!";
mhp -= 20;
}
else if (wa == "dragonslash" && m == "Dragon") {
cout << "Warrior used Dragon Slash!";
mhp -= 80;
}
else if (wa == "dragonslash" && (m == "Troll" || m == "Goblin")) {
cout << "Warrior's attack did no damage!";
}
if (ma == "icicledrop") {
cout << "Mage used Icicle Drop!";
mhp -= 30;
mahp -= 10;
whp -= 10;
hhp -= 10;
}
else if (ma == "flamesofhell") {
cout << "Mage used Flames of Hell!";
mhp -= 75;
mahp -= 50;
whp -= 50;
hhp -= 50;
}
else if (ma == "punch") {
cout << "Mage used Punch!";
mhp -= 5;
}
}
}
Upvotes: 0
Views: 558
Reputation: 283634
In dgn()
, you have
int mhp;
which is sensible, because it is a numeric quantity.
But then your helper function declares
string mhp
in the argument list, which should have caused a type mismatch error between actual and formal parameters in the function call
dgnrd(m, mhp, whp, mahp, hhp);
Fix that to int& mhp
and several problems will go away at once.
Note that &
which creates a reference. This makes the function share the variable with its caller, so that changes are made to the caller's copy. Otherwise (in pass-by-value) all changes inside the function simply disappear when the function returns.
Upvotes: 2
Reputation: 68715
It seems you are running the -= operator on a string instead of int. mhp
is a string
and hence the following statements is causing compilation error:
mhp -=
Upvotes: 0
Reputation: 227420
The reason is that std::string
has no operator -=
. There is +=
, which appends to an existing string, but the semantics of an operator -=
wouldn't be clear.
Besides that obvious problem, the types of the parameters of dgnrd
function do not match those of the arguments you pass it.
Upvotes: 1