Setter doesn't work

I am writting the Graduation program from Beginner Exercises and my setter doesn't work somehow. When I print the value in the setter it shows that it has inremented, but if I print the value outside the function, nothing has changed.

Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  using namespace std;

class Bunnie
{
    public:
        enum Sex {male, female};
        enum Color {white, brown, black, spotted};
        enum BunnieNames {Oreo, Baby, Daisy, Thumper, Lily, Snowball, Peanut, Nibbles, Smokey, Isabelle};

        Bunnie();
        virtual ~Bunnie();
        unsigned short getAge();
        void setAge(unsigned short age);

        friend ostream& operator<<(ostream& os, const Bunnie& dt);
    protected:

    private:
        Sex sex;
        Color color;
        unsigned short age;
        BunnieNames name;
        bool radioactive_mutant_vampire_bunny;
};


cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using namespace std;

class Bunnie
{
    public:
        enum Sex {male, female};
        enum Color {white, brown, black, spotted};
        enum BunnieNames {Oreo, Baby, Daisy, Thumper, Lily, Snowball, Peanut, Nibbles, Smokey, Isabelle};

        Bunnie();
        virtual ~Bunnie();
        unsigned short getAge();
        void setAge(unsigned short age);

        friend ostream& operator<<(ostream& os, const Bunnie& dt);
    protected:

    private:
        Sex sex;
        Color color;
        unsigned short age;
        BunnieNames name;
        bool radioactive_mutant_vampire_bunny;
};


operator<<:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
ostream &operator<< (ostream &os, Bunnie const &b) {
    os << "Sex: ";
    switch(b.sex){
    case Bunnie::male:
        os << "male";
        break;
    case Bunnie::female:
        os << "female";
        break;
    }
    os << ", Color: ";
    switch(b.color){
    case Bunnie::white:
        os << "white";
        break;
    case Bunnie::brown:
        os << "brown";
        break;
    case Bunnie::black:
        os << "black";
        break;
    case Bunnie::spotted:
        os << "spotted";
        break;
    }
    os << ", Age: " << b.age;
    os << ", Name: ";
    switch(b.name){
    case Bunnie::Oreo:
        os << "Oreo";
        break;
    case Bunnie::Baby:
        os << "Baby";
        break;
    case Bunnie::Daisy:
        os << "Daisy";
        break;
    case Bunnie::Thumper:
        os << "Thumper";
        break;
    case Bunnie::Lily:
        os << "Lily";
        break;
    case Bunnie::Snowball:
        os << "Snowball";
        break;
    case Bunnie::Peanut:
        os << "Peanut";
        break;
    case Bunnie::Nibbles:
        os << "Nibbles";
        break;
    case Bunnie::Smokey:
        os << "Smokey";
        break;
    case Bunnie::Isabelle:
        os << "Isabelle";
        break;
    }
    os << ", Radioactive mutant vampire bunny: " << b.radioactive_mutant_vampire_bunny;

  return os;
}
The second snippet is not your .cpp file.

The only setter you appear to have is setAge().

You also haven't shown your main, or where/how you call the setter. It's impossible to say what is going on without seeing the code for your setter and where you call it.



Ok, this is my main file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>

#include "Bunnie.h"


using namespace std;

ostream &operator<< (ostream &os, Bunnie const &b);
void initialization();
void turns();
void printBunnies();
void incrementAge();

vector<Bunnie> bunnies;

int main(){
    initialization();
    turns();
    return 0;
}

void initialization(){
    for(int i = 0; i < 5; i++){
        bunnies.push_back(Bunnie());
    }
}

void turns(){
    for(int i = 0; i < 10; i++){
        incrementAge();
        printBunnies();
    }
}

void printBunnies(){
    for(Bunnie b : bunnies) {
        cout << b << '\n';
    }
    cout << endl;
}

void incrementAge(){
    for(Bunnie b : bunnies) {
        b.setAge(b.getAge()+1);
    }
}
And why is the second file not the main file?
Line 44: b is a copy. Any changes to b are lost on the next iteration of the loop.

Use an iterator to iterate through bunnies.
Topic archived. No new replies allowed.