Re: Another STL ?
sd2004 wrote:[color=blue]
> ////////////////////// QUESTION //////////////////////////
> // how can I "store" value of variable "test" into member
> "number" of class "dog",[/color]
Just to be clear on the terminology here - 'dog' is an object, which is
of class 'dog_info_class'.
However, you also have other objects of class dog_info_class, which are
stored in the vector. Note that each of these objects is separate from
the object called 'dog'; when you call v.push_back(dog), that adds a
_copy_ of the object called 'dog' into the vector.
[color=blue]
> // so that I can use iter->number to print out the data or other
> purposes.[/color]
If you want to do anything to the objects in the vector, you must
change these objects, not the object called 'dog'. So, for instance,
you could set the value of 'number' for the specific object referred to
by 'iter' by doing:
iter->number = test;
[color=blue]
> // What I have in mind is to use push_back as I did in line 40
> "v.push_back(dog)"[/color]
If you do this, it will add another object to the vector. Is that what
you want to do?
Also, I have a question about this function:
[color=blue]
> int dog_info_class::dog_age (const int YearBorn){
> int age = (CurrentYear - YearBorn)*1;
> return age;
> }[/color]
Why are you passing it a value for 'YearBorn'? Each dog_info_class
object already has a value for YearBorn. So you can use that value:
int dog_info_class::dog_age()
{
return CurrentYear - YearBorn;
}
Then, in your loop, change:
[color=blue]
> test=dog.dog_age(iter->YearBorn)*variable;[/color]
to:
test = iter->dog_age() * variable; |