3

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.

Kim Stacks
  • 10,946
  • 29
  • 130
  • 248

2 Answers2

4

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.

Community
  • 1
  • 1
juanchopanza
  • 210,243
  • 27
  • 363
  • 452
  • I have added the changes as you mentioned. http://pastebin.com/9anijwzQ But I am getting errors regarding the unique_ptr. But I have included memory. So I am not sure what the issue is. http://pastebin.com/wP6vEVn6 is the error message. – Kim Stacks Nov 04 '12 at 13:33
  • Thank you, I got it to work with your help. I would like to ask a few things to clarify. I understand the need to have std::string speak() as a virtual method. I do not understand why you made it = 0; secondly, I am not sure what is the purpose of virtual ~Animal() {} – Kim Stacks Nov 06 '12 at 02:06
  • 1
    @kimsia I added some links that should answer your questions. – juanchopanza Nov 06 '12 at 06:58
  • dude, you are awesome! I got some rude nonsense from some other guy in SO who jumped on me for wanting someone to write some C++ code since I am not a C++ guy at all. You are totally different from him. Props to you, my man!! Now i know that ~ means destructor and = 0 helps to allow overriding. You are way too cool, juanchopanza!! By the way, this is what I have done with your help. http://stackoverflow.com/a/13243374/80353 you are wonderful!! – Kim Stacks Nov 06 '12 at 16:11
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.

amaurea
  • 4,691
  • 19
  • 33