c++错误:“operator-=”不匹配
c++ error: no match for ‘operator-=’
我对c++还很陌生。作为一个项目,我正在重写我用python编写的一个小游戏(我从来没有让它正确工作过)。在编译过程中,我得到了以下错误:错误:与"operator-="不匹配
我知道这个运算符存在于c++中,那么为什么我会得到这个错误呢?
代码:
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;
}
}
}
在dgn()
中,您有
int mhp;
这是合理的,因为它是一个数字量。
但是你的助手函数声明
string mhp
在参数列表中,这应该导致函数调用中实际参数和形式参数之间的类型不匹配错误
dgnrd(m, mhp, whp, mahp, hhp);
将其修复到int& mhp
,几个问题将立即消失。
注意创建引用的&
。这使得函数与其调用方共享变量,以便对调用方的副本进行更改。否则(在传递值中)当函数返回时,函数内部的所有更改都会消失。
原因是std::string
没有运算符-=
。有+=
,它附加到现有的字符串,但运算符-=
的语义并不清楚。
除了这个明显的问题之外,dgnrd
函数的参数类型与您传递的参数类型不匹配。
您似乎在字符串而不是int上运行-=运算符。mhp
是string
,因此以下语句导致编译错误:
mhp -=
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- Valarray 切片数组与 'operator*' 不匹配
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 与"operator<<"不匹配(操作数类型'std::ostream'
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 使用标准::时间::high_resolution_clock时"operator ="不匹配
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- 错误:尝试减去字符串时'operator-'不匹配
- 调用模板方法会导致'operator<'不匹配
- \main.cpp|103|error: 与"operator<<"不匹配(操作数类型为"std::ostream {aka std::basic_ostream<char&g
- 使用 #omp 时'operator -'不匹配
- 尝试使用 fstream 将字符写入文件:'operator<<'不匹配
- C++ 将文件读取到数组错误:'operator>>'不匹配
- 有没有办法修复此错误:与'operator[]'不匹配(操作数类型为 'empl' 和"int")
- 错误与'operator='不匹配(操作数类型为 'Person' 和"Person*")
- 我得到"将值分配给结构数组 iin 时与'operator='不匹配C++
- 在地图中搜索值时'operator='不匹配
- 错误:'operator<<'和"运算符>>"不匹配。请帮忙?