I have a virtual class Animal, and I want to store Animal objects in a vector and be able to access any Animal object in this vector by index.
I declared my vector: std::vector <Animal*> anim;
Am able to add to my vector using:
void Shelter::add_animal(Animal& animal){
anim.push_back(&animal);
}
But when I try to access my animals using:
Animal& Shelter::animal(int index){
return anim[index];
}
I run into an error.
shelter.cpp:18:28: error: invalid initialization of reference of type ‘Animal&’ from expression of type ‘__gnu_cxx::__alloc_traits<std::allocator<Animal*> >::value_type {aka Animal*}’
return _anim[index];
I'd like to understand why I can't access this vector the same way I've accessed vectors in the past. Thanks!
You are storing pointers to Animals, and not Animals. dereference the pointer.
Animal& Shelter::animal(int index){
return *anim[index];
}
/edit
My original suggestion was incorrect, but this is why I made that suggestion....
You might want to consider changing the interface for:
void Shelter::add_animal(Animal& animal);
This pretty much guarantees a maintenance nightmare. It implies I can do:
void makePig(Shelter& s)
{
Pig littlePiggy;
s.add_animal(littlePiggy);
}
Which is how I originally thought the code would be used (hence my parsing error when answering the question). Personally speaking, I'd argue this would be better:
void Shelter::add_animal(Animal* animal);
The simple reason being that with this interface, all compilers will spit out a warning if you do this:
Pig littlePiggy;
s.add_animal(&littlePiggy); //< Warning! taking address of temp var.
With the original interface, no such warning will ever be generated, and the interface implies that you will be copying data into it (rather than storing a pointer to some var)