c++ error no matching function
Solution 1
There are two problems.
First, your vector contains Animal
objects, and you are trying to fill it with pointers to Animal
derived types. Animal
and Animal*
are not the same type, so that operation wouldn't usually compile.
Second, Animal
has no method speak()
. If you were to push derived types of Animal
into the vector, you would get object slicing. You can avoid it by having your vector hold smart pointers to Animal
, for example std::vector<std::unique_ptr<Animal>>
. But you still need to give Animal
a speak()
virtual method. For example:
class Animal {
public:
std::string name;
Animal (const std::string& givenName) : name(givenName) {}
virtual std::string speak () = 0;
virtual ~Animal() {}
};
int main() {
std::vector<std::unique_ptr<Animal>> animals;
animals.push_back( std::unique_ptr<Animal>(new Dog("Skip")) );
animals.push_back( std::unique_ptr<Animal>(new Cat("Snowball")) );
}
Where I have made Animal::speak()
a pure virtual method and given Animal
a virtual destructor.
See when to use virtual destructors and when should a virtual method be pure.
Solution 2
You should declare your vector
as vector<Animal*>
if you want to push Animal*
like skip
to it. And you do want pointers in it in order to be able to use polymorphism. Additionally, your base class animal also needs a speak()
method - otherwise you can't call that on an object which you at compile time only know to be an Animal
. It should work as you expect once those changes have been made.
Kim Stacks
I build end-to-end workflow solutions to process data such as BOQ (Bill of Quantities) for businesses in telecomms, civil engineering, and construction.
Updated on June 04, 2022Comments
-
Kim Stacks almost 2 years
this is my code
#include <iostream> #include <vector> #include <memory> #include <tr1/memory> using namespace std; class Animal { public: string name; Animal (const std::string& givenName) : name(givenName) { } }; class Dog: public Animal { public: Dog (const std::string& givenName) : Animal (givenName) { } string speak () { return "Woof, woof!"; } }; class Cat: public Animal { public: Cat (const std::string& givenName) : Animal (givenName) { } string speak () { return "Meow..."; } }; int main() { vector<Animal> animals; Dog * skip = new Dog("Skip"); animals.push_back( skip ); animals.push_back( new Cat("Snowball") ); for( int i = 0; i< animals.size(); ++i ) { cout << animals[i]->name << " says: " << animals[i]->speak() << endl; } }
these are my errors:
index.cpp: In function ‘int main()’: index.cpp:36: error: no matching function for call to ‘std::vector<Animal, std::allocator<Animal> >::push_back(Dog*&)’ /usr/include/c++/4.2.1/bits/stl_vector.h:600: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Animal, _Alloc = std::allocator<Animal>] index.cpp:37: error: no matching function for call to ‘std::vector<Animal, std::allocator<Animal> >::push_back(Cat*)’ /usr/include/c++/4.2.1/bits/stl_vector.h:600: note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Animal, _Alloc = std::allocator<Animal>] index.cpp:40: error: base operand of ‘->’ has non-pointer type ‘Animal’ index.cpp:40: error: base operand of ‘->’ has non-pointer type ‘Animal’
What I want to do:
I just want to use a dynamic data structure that will go through a list of possible Animal objects.
I am trying to learn this polymorphism concept in C++ syntax.
I am familiar with Java and PHP but significantly less so with C++.
UPDATE:
I have added the changes as mentioned by one of the answers. http://pastebin.com/9anijwzQ
But I am getting errors regarding the unique_ptr. I have included memory. So I am not sure what the issue is.
http://pastebin.com/wP6vEVn6 is the error message.