vector<object> not declared in class method error

i am trying to increment the object age, which the objects are stored in a vector. I think i am starting to get lost in the code as it gets longer to do the proper things. The error i get:

/home/metulburr/Documents/cplusplus/radioactive_bunnies/src/Control.cpp|66|error: ‘class std::vector<Bunny>’ has no member named ‘age’|


the method in Control.cpp
1
2
3
4
5
6
void Control::add_age_update(){
    //add 1 every turn
    for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
        bunny_obj_list.age++;
    }
}



Control.cpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "../include/Control.h"
#include "../include/Bunny.h"
#include <vector>
#include <iostream>

using namespace std;

Control::Control(){
    //std::vector<Bunny> bunny_obj_list;
    bunny_count = 0; //bunny object count
    loop_count = 2012; // year
    age_to_die = 9; // +1
    RA_number = 0; //number of bunnies with radiation
    total_old_death_count = 0;
    total_RA_death_count = 0;
    total_birth_count = 0;
    female_count = 0;
    starvation = 0;
}

Control::~Control()
{
    //dtor
}

void Control::create_obj(int num){
    for(int i=0; i < num; i++){
        Bunny obj;
        bunny_obj_list.push_back(obj);
        total_birth_count++;
        bunny_count++;
        if (obj.get_RA())
            RA_number++;
        display(obj, "born");
    }
}

void Control::display(Bunny object, std::string reason){
    std::string display_reason;
    std::string display_string;
    std::string disp_RA;
    if (reason == "born")
        display_reason = "was born.";
    else if (reason == "old")
        display_reason = "has died of old age.";
    else if (reason == "RA")
        display_reason = "was killed by radiation.";

    if (object.get_RA()){
        disp_RA = "Radioactive ";
    }
    else{
        disp_RA = "";
    }

    display_string =  disp_RA + "bunny " + object.get_name() + " " + display_reason + "\n" + object.get_sex() + " " + object.get_color();
    cout << display_string <<endl;


    sleep(1);
}

void Control::add_age_update(){
    //add 1 every turn
    for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
        bunny_obj_list.age++;
    }
}

void Control::update(){
    add_age_update();





}

/*int get_RA_number(){
    return RA_number;
}*/



Control.h
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
#ifndef CONTROL_H
#define CONTROL_H

#include <vector>
#include <string>
#include "../include/Bunny.h"

//using namespace std;


class Control{
    //private:
    public:
        std::vector<Bunny> bunny_obj_list;
        int bunny_count, loop_count, age_to_die, RA_number, total_old_death_count;
        int total_RA_death_count, total_birth_count, female_count, starvation;
    //public:
        Control();
        ~Control();
		int get_bunny_count();
		int get_loop_count();
		int get_age_to_die();
		int get_RA_number();
		int get_total_old_death_count();
		int get_total_RA_death_count();
		int get_total_birth_count();
		int get_female_count();
		int get_starvation();

		void create_obj(int num);
		void display(Bunny object, std::string reason);
		void add_age_update();
		void update();
};

#endif // CONTROL_H 



Bunny.h
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
#ifndef BUNNY_H
#define BUNNY_H

#include <string>



class Bunny{
    private:
        std::string sex;
        std::string color;
        int age;
        std::string name;
        bool RA; //radioactive
    public:
        Bunny();
        ~Bunny();

        std::string get_sex();
        std::string set_sex();

        std::string get_color();
        std::string set_color();

        int get_age();
        //int set_age();

        std::string get_name();
        std::string set_name();

        bool set_RA();
        bool get_RA();

        int randnum(int max);

};

#endif // BUNNY_H

the error message of the compiler is clear enough. Class std::vector<Bunny> has no such a method as age.
I think that instead of

1
2
3
4
5
6
void Control::add_age_update(){
    //add 1 every turn
    for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
        bunny_obj_list.age++;
    }
}


you have to write

1
2
3
4
5
6
void Control::add_age_update(){
    //add 1 every turn
    for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
        it->age++;
    }
}


provided that class Bunny has public data member age.
Last edited on
Yes!! that worked after i changed all members to public. I never used the this -> before. So I wasn't sure how to access the iterator object members.

I think my brain was fried after staying up all night doing c++ for 8 hours, being a noob. I think i need to take a break. :)

Thanks
Last edited on
Yes!! that worked after i changed all members to public


You should avoid doing this - it is a quick fix for now, but bad in the future. I am sure that changing all the members to public was not quite what Vlad had in mind.

All the members should be private, and provide public functions to access them. Although don't fall into the trap of providing getter / setters for each one (because that is the same as having public members), try to think how it might happen in reality and provide functions that do this. You can also make use of constructors to set values initially, which is ideal for bunnies as presumably the sex & colour doesn't change once set. Remember that a member function has access to all the private member variables directly.

For your code, you could have an IncrementAge function in the Bunny class.

You can still use the -> operator to call functions.

HTH
All the members should be private, and provide public functions to access them Although don't fall into the trap of providing getter / setters for each one (because that is the same as having public members)

Well thats good to know. The only reason i made setter getter functions was it seemed that i kept getting errors saying that "variable is private" when trying to access them or change them inside its own class. But making getter functions seemed to have access to changing / getting them. And I am assuming you mean to put all the setter functions inside the constructor instead? Doesn't setter functions separate the logic more? or it is to assume a lot of logic inside the constructor?

Remember that a member function has access to all the private member variables directly.

Is there sonthing you have to explicitly do to have this happen? for example layout:

if (member.variable_bool) execute code;
execpt i get errors saying that member.variable_bool is private, and all i am checking is if it is true or not. (not even changing it for example)
From my understanding you should be able to do this inside the member.variable's member functions within the same class?
Last edited on
When I was giving the advice I wanted to point the wrong syntax you are using. Class std::vector has no such member as age. So the record

bunny_obj_list.age++;

is incorrect where bunny_obj_list is a vector. It is Bunny that has that data member. So you shall access alements of the vector through iterators.
As for private data members then any member of a class has access to other members of the same class.
omg, i just got it. thanks. lol I was trying to access member age from the vector not the object in the vector. *metulburr slaps forehead

Thanks
i was trying to think of a way to make the looping vector a function taking an argument as the member to incrment:

I was thinking something like:
1
2
3
4
5
6
void Control::loop_vector(Bunny member=NULL){
    //add 1 every turn
    for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
        it->member++;
    }
}

but the error:
/home/metulburr/Documents/cplusplus/radioactive_bunnies/src/Control.cpp|71|error: default argument for ‘Bunny member’ has type ‘long int|


Also i was trying to erase the element by index based on the condition of the object age greater than the age_to_die
1
2
3
4
5
6
7
8
void Control::kill_old_update(){
    for (std::vector<Bunny>::iterator it = bunny_obj_list.begin(); it != bunny_obj_list.end(); ++it){
        if(it->age > age_to_die){
            display(it, "old");
            bunny_obj_list.erase(it);
        }
    }
}


but i am not sure exactly what the error msg means?

/home/metulburr/Documents/cplusplus/radioactive_bunnies/src/Control.cpp|38|note: no known conversion for argument 1 from ‘std::vector<Bunny>::iterator {aka __gnu_cxx::__normal_iterator<Bunny*, std::vector<Bunny> >}’ to ‘Bunny’|




EDIT:
actually i just madea new thread as it is getting off topic
http://www.cplusplus.com/forum/beginner/89496/
Last edited on
Topic archived. No new replies allowed.