2013-07-30 106 views
0

我對C++相當陌生。作爲一個項目,我重寫了一個我用python編寫的小遊戲(我從來沒有正確使用它)。在編譯過程中,我得到這個錯誤:錯誤:'operator- ='不匹配''C++錯誤:'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; 
     } 
    } 
} 
+0

元音。不要害怕他們。 – DanielKO

回答

2

dgn(),你有

int mhp; 

這是明智的,因爲它是一個數字量。

但隨後你的助手函數聲明

string mhp 
參數列表

,本應在函數調用

dgnrd(m, mhp, whp, mahp, hhp); 

修復造成的實際和形式參數之間的類型不匹配錯誤,爲int& mhp幾個問題一下子就會消失。

請注意,&它創建一個引用。這使該函數與調用者共享變量,以便對調用者的副本進行更改。否則(按值傳遞)函數內部的所有更改都會在函數返回時消失。

1

的原因是std::string沒有運營商-=。有+=,它追加到現有的字符串,但操作符-=的語義不清楚。

除了這個明顯的問題,dgnrd函數的參數類型與您傳遞的參數的類型不匹配。

0

看來你正在對字符串而不是int運行 - =運算符。 mhpstring,因此下面的語句是導致編譯錯誤:

mhp -= 
相關問題