2014-02-16 68 views
-2

我正在寫一個程序,需要「模擬」兩個pokemon之間的戰鬥。我有一個userBuild類來創建寵物小精靈。每當用戶創建一個寵物小精靈時,都會調用這個類。在那裏我遇到了一個錯誤的地方是,當我添加getter和setter方法:C++正確的getter和setter

#include "Pokemon.h" 
#include <string> 
#include "Dice.h" 

Pokemon::Pokemon() { 
    healthPoints = attackL = defL =0; 
     std::string Pname= ""; 
     d20=Dice(20); 
     d6=Dice(6); 
    } 

bool Pokemon::attack(Pokemon opponent){ 
int attackBonus = d20.roll(); 
int defenseBonus = d20.roll(); 


std::cout<<Pname<<" rolls an attack bonus of "<<attackBonus<<std::endl; 
std::cout<<this -> Pname<<" rolls an defense bonus of "<<defenseBonus<<std::endl; 

//if the attackLevel+attackBonus of the attacker is greater than the the defenseLevel+defenseBonus of the defender, then roll for damage. Otherwise the attack misses 

if (attackL+attackBonus >= opponent.defL+defenseBonus){//Pokemon 1 attack 
    int roll1, roll2, roll3; //bonus 3 d6 rolls 
    roll1 = d6.roll(); 
    roll2 = d6.roll(); 
    roll3 = d6.roll(); 

    int totalDamage = roll1 + roll2 + roll3; 

    std::cout<<"The attack hits dealing 3-D6 damage!"<<std::endl; 
    std::cout<<"The rolls are "<<roll1<<", "<<roll2<<", and "<<roll3<<" totalling: "<<totalDamage<<" damage!"<<std::endl; 
    std::cout<<opponent.Pname<<" has"<<(opponent.healthPoints)- totalDamage<<"hit points left"<<std::endl; 

    if (opponent.healthPoints <= 0){ 
     return true; 
    } 

} 
else if (attackL+attackBonus <= opponent.defL+defenseBonus){ 
    std::cout<<"The attack missed"<<std::endl; 
} 
return false; 
} 

void Pokemon::userBuild(){ 


    std::cout<< "Please name your Pokemon: "; 
    std::cin>>Pname; 

    std::cout<< "How many hit points will it have? (1-50): "; 
    std::cin>>healthPoints; 

    std::cout<<"Split fifty points between attack level and defense level"<<'\n'; 
    std::cout<<"Enter your attack level (1-49): "; 
    std::cin>>attackL; 

      if (attackL > 49) { //checks that the input for attack level is within the acceptable range 
       std::cout<<"Sorry. The attack level must be between 1 and 49: "; 
       std::cin>>attackL; 
      } 
       else if (attackL <= 0) { 
       std::cout<<"Sorry. The attack level must be between 1 and 49: "; 
       std::cin>>attackL; 
       } 


    std::cout<<"Enter your defense level (1-30): "<<std::endl; 
    std::cin>>defL; 



      if (defL > 30) { //checks that the input for defense level is within the acceptable range 
       std::cout<<"Sorry. The defense level must be between 1 and 30: "; 
       std::cin>>defL; 
      } 

       else (defL <= 0);{ 
       std::cout<<"Sorry. The defense level must be between 1 and 30: "; 
       std::cin>>defL; 
       } 



} 



//initiazion of getters 
    int Pokemon::getHP(){ 

     return healthPoints; 
    } 
    int Pokemon::getAttackLevel(){ 
     return attackL; 
    } 
    int Pokemon::getDefenseLevel(){ 
     return defL; 
    } 
    std::string Pokemon::getname(){ 
     return Pname; 
    } 

    //initiation of setters 
    void setHP(int HP){ 
     healthPoints=HP; 
    } 
    void setAttackLevel(int attackLevel){ 
     attackL=attackLevel; 
    } 
    void setDefenseLevel(int defenseLevel){ 
     defL=defenseLevel; 
    } 
    void setname(std::string name){ 
     Pname= name; 
    } 
+2

你碰到什麼錯誤? –

+1

此外,這行代碼不會執行任何'std :: string Pname =「」;',創建一個名爲'Pname'的新變量,然後將其拋出。 –

+0

'g ++ -Wall -c「Pokemon.cpp」 Pokemon.cpp:在函數'void setHP(int)'中: Pokemon.cpp:106:4:錯誤:'healthPoints'未在此範圍內聲明 healthPoints =生命值;' 我不確定這是語法問題還是獲取者和設置者的位置 – rubito

回答

1

你沒有取得setHP的定義功能口袋妖怪類的成員。它應該是

void Pokemon::setHP(int HP){ 
     healthPoints=HP; 
    } 

那裏的其他功能應該也應該在這個範圍內。

+0

非常感謝!這照顧了所有問題!如果問題不多,您是否看到我的代碼中可以更改的任何內容?我是一個初級中學,我想提高。所以任何意見都會很棒!再次感謝 – rubito

+0

一般的代碼審查將更適合CodeReview.SE之類的東西。其中一條建議是查看(圖書清單)[http://stackoverflow.com/q/388242],看看你的課程是否包含書。 –

+0

書籍清單?那是什麼?我試着按照你的鏈接,但它似乎被打破。 – rubito