-1

I'm writing a program that needs to "simulate" a battle between two pokemon.I have a userBuild class to create the pokemon. This class is called everytime the user creates a pokemon. The place where I'm running into an error is when I add the getters and setters:

#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;
    }
ajay
  • 9,402
  • 8
  • 44
  • 71
rubito
  • 327
  • 4
  • 15
  • 2
    What error are you running into? – David Schwartz Feb 16 '14 at 20:38
  • 1
    Also, this line of code does nothing `std::string Pname= "";`, creating a new variable called `Pname` and then throwing it awway. – David Schwartz Feb 16 '14 at 20:39
  • 'g++ -Wall -c "Pokemon.cpp" Pokemon.cpp: In function ‘void setHP(int)’: Pokemon.cpp:106:4: error: ‘healthPoints’ was not declared in this scope healthPoints=HP;' I'm not sure if this is an issue with syntax or the location of the getters and setters – rubito Feb 16 '14 at 20:42

1 Answers1

1

You've not made the definition of the setHP function a member of the Pokemon class. It should be

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

The other functions there should presumably also be in this scope.

  • Thanks a lot! That took care of all the issues! If is not asking a lot, do you see anything in my code that could be changed? I'm a beginner taking a mid-level class and I would like to improve. So any comments would be great! Thanks agains – rubito Feb 16 '14 at 20:49
  • A general code review would be more suited to something like CodeReview.SE. One piece of advice is to take a look at the (Book List) [http://stackoverflow.com/q/388242] and see if there's a book not covered by your course. – The Forest And The Trees Feb 16 '14 at 20:52
  • Book list? What is that? I tried following your link but it appears to be broken. – rubito Feb 16 '14 at 20:59
  • There's an extra `]` in the link. Try removing that. – Ben Voigt Feb 16 '14 at 21:14
  • Ohh I see! I have C++ Primer and I've been reading that since the reviews said that it was a great way to get started with C++ – rubito Feb 16 '14 at 21:16