2014-02-16 70 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