我正在寫一個程序,需要「模擬」兩個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;
}
你碰到什麼錯誤? –
此外,這行代碼不會執行任何'std :: string Pname =「」;',創建一個名爲'Pname'的新變量,然後將其拋出。 –
'g ++ -Wall -c「Pokemon.cpp」 Pokemon.cpp:在函數'void setHP(int)'中: Pokemon.cpp:106:4:錯誤:'healthPoints'未在此範圍內聲明 healthPoints =生命值;' 我不確定這是語法問題還是獲取者和設置者的位置 – rubito