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++;
}
}
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.
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.
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?
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.
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 ‘longint’|
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’|