4

I wrote a program in c++ with 2 classes. Basically "HeroList" just is a vector of "Hero" elements. Now I want to change the values each hero has given (in function "change"). But it doesn't work. It only works if I call the function with the "Hero" Objects directly as parameter(try 1). But when I use the "heroList"-elements as parameter (try 2) it changes the values only during the function is active and when its over they reset. I guess it has something to do with the usage of reference, but I cant find my mistake.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Hero
{

private:
    string name;        //Hero-Name
    int value;          //Hero-Value

public:

    Hero(string name = "std", int value = 0) {

        this->name = name;
        this->value = value;
    }                           

    void setValue(int i) {
        if (i == 1) {                   //If 1 -> value - 1
            if (value != 0) {           //If value already 0 nothing happens
                value = value - 1;
            }
        }
        if (i == 2) {                   //If 2 -> value + 1
            if (value != 10) {          //If value already 10 nothing happens
                value = value + 1;
            }

        }
    }

    float getValue() {
        return value;
    }

    string getName() {
        return name;
    }

    void print() {
        cout << name << ": " << value << endl;
    }
};



class HeroList
{
private:
    string name;
    vector<Hero> Heroes;

public:
    HeroList(string name = "std", vector<Hero> Heroes = {}) {

        this->name = name;
        this->Heroes = Heroes;
    }

    string getName() {
        return name;
    }

    vector<Hero> getHeroes() {
        return Heroes;
    }

};



void change(Hero& x, Hero& y) {         //Function to change the Hero-Values permanently (not only during the function is running)
    
        x.setValue(2);
        y.setValue(1);

}



int main() {

    Hero Alice("Alice", 5);
    Hero Bob("Bob", 5);

    vector<Hero> duo = { Alice,Bob };

    HeroList Duo("Duo", duo);

    //try 1 
    change(Alice, Bob);
    cout << "try 1: " << endl;

    cout << Alice.getName()<<": " << Alice.getValue() << endl;
    cout << Bob.getName() << ": " << Bob.getValue() << endl << endl;
    
    //try 2
    change(Duo.getHeroes()[0], Duo.getHeroes()[1]);

    cout << "try 2: " << endl;
    cout << Duo.getHeroes()[0].getName() << ": " << Duo.getHeroes()[0].getValue() << endl;
    cout << Duo.getHeroes()[1].getName() << ": " << Duo.getHeroes()[1].getValue() << endl;

    return 0;
}

Output:

try 1:
Alice: 6
Bob: 4

try 2:
Alice: 5
Bob: 5

How it should be:

try 1:
Alice: 6
Bob: 4

try 2:
Alice: 6
Bob: 4
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
M4GI3R
  • 85
  • 1
  • 5

2 Answers2

5

You can update getHeroes() to return a reference to the Heroes vector to get the behavior you want:

vector<Hero>& getHeroes() {
    return Heroes;
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
5

the problem is here:

in the HeroList class

vector<Hero> getHeroes() {
    return Heroes;
}

you are returning a copy of the vector<hero>.... try instead returning a reference

vector<Hero>& getHeroes() {
    return Heroes;
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97