0

I'm making a pokemon battle code. Here are codes that I've made. First, I define Pokemon class.

(pokemon.h)

class Pokemon{ ... 
private:
    int hp;
public:
    int gethp();
    void sethp(int n); 
...}

(pokemon.cpp)

... 
int Pokemon::gethp(){return hp;}
void Pokemon::sethp(int n){hp = n;} 
...

Next, I define Trainer class. Trainer has a list of pokemons.

(trainer.h)

class Trainer: public Pokemon {
private:
    Pokemon pokemons[3];
...
public:
    Pokemon getpokemon(int n);
    void setpokemons(int n, int m, int i);
...

(trainer.cpp)

Pokemon Pikachu(..., 160, ...)               //Pikachu's hp is defined to be 160.
...
Pokemon makepokemon(int n) {        
    if (n == 1) { return Pikachu; }
....
}
Pokemon Trainer::getpokemon(int n) { return pokemons[n-1]; }
void Trainer::setpokemons(int n, int m, int i) {
    pokemons[0] = makepokemon(n);
    pokemons[1] = makepokemon(m);
    pokemons[2] = makepokemon(i);
}
...

Now, when I use gethp/sethp in the main fucntion, I have a problem.

(main part)

Trainer me;
me.setpokemons(1, ...);           // My first pokemon is pikachu then.

...

cout << me.getpokemon(1).gethp();             //This gives 160.
me.getpokemon(1).sethp(80);                   //I want to change Pikachu's hp into 80.
cout << me.getpokemon(1).gethp();             //Still this gives 160.

The problem is that sethp is not working. I guess that I need to use call by reference at some point to fix this, but I don't get how I should do.

How can I fix this problem?

  • 1
    please post real code, a [mcve]. Code that you made incorrect by introducing syntax errors all over the place is of little use when we want to help you with your real code. – 463035818_is_not_an_ai Oct 15 '20 at 13:16
  • 3
    You're on the right track, but you don't need *call* by reference, you need to *return* a reference. (Although I think an interface with `increaseHp` and `decreaseHp` would be more better. Getters and setters are not-nice.) – molbdnilo Oct 15 '20 at 13:16
  • 6
    `class Trainer: public Pokemon {` if `Trainer` inherits from `Pokemon`, you're saying that a Trainer *is a* Pokemon. Is this correct? – scohe001 Oct 15 '20 at 13:16
  • Sorry my real code contains other many information so it is very long. So I've just posted the minimal amount of contents. – Moon Ju Hong Oct 15 '20 at 13:48
  • No. Trainer is not a Pokemon.But it has a list of three pokemons. Is the nested structure wrong? – Moon Ju Hong Oct 15 '20 at 13:50
  • Which reference should I return? I have multiple objects so it's difficult for me to decide what I need to change. – Moon Ju Hong Oct 15 '20 at 13:52
  • 1
    @MoonJuHong if Trainer is not a Pokemon, then it should not inherit from Pokemon. Read more on "is-a" vs "has-a" here: https://stackoverflow.com/a/36162730/2602718 – scohe001 Oct 15 '20 at 13:57

1 Answers1

1
me.getpokemon(1).sethp(80)

Let's walk through this--you're calling me.getpokemon(1), which returns a copy of a Pokemon from me's pokemons array. You then call sethp on the copy, setting its hp to 80. And then, since the copy isn't saved anywhere, you never see it again until it's destroyed...

What you want instead, is to cause getpokemon(1) to return a reference to the pokemons item insetad of a copy, so that when you call sethp(), you're modifying the original. You can do that by changing the function to return a reference instead of a copy:

Pokemon& Trainer::getpokemon(int n) { return pokemons[n-1]; }
//    ^^^
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Oh I was changing nothing in the previous way. Okay, now I'm getting not the copy, but its reference so that I can change them. Now I see the picture. Thank you for your clear explanation! – Moon Ju Hong Oct 15 '20 at 14:54