In this program, I am supposed to call animal.at(0)->makeSound()
in main()
and have it return "Woof"
from the public member function makeSound()
for Dog
. However, when I compile the code as written, it gives me an error:
base operand of '->' has non-pointer type
While I know there are other ways around this, I am not allowed to modify any of this code except for the vector
type and the element list of the vector
, as this is a practice problem from an old homework I got wrong.
If somebody could tell me how to set up the array properly (vector <MISSING_TYPE> animal {MISSING VECTOR ELEMENT};
) so that it will compile, you will be saving me for finals. What I have now is currently incorrect.
#include <iostream>
#include <vector>
using namespace std;
class Animal
{
public:
virtual void makeSound() {
cout << "Animal!" << endl;
}
};
class Dog : public Animal
{
public:
void makeSound() {
cout << "Woof" << endl;
}
};
int main()
{
Dog dog;
vector<Animal> animal {dog};
animal.at(0)->makeSound();
return 0;
}