Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:41:55+00:00 2026-06-13T19:41:55+00:00

this is my code #include <iostream> #include <vector> #include <memory> #include <tr1/memory> using namespace

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:41:58+00:00Added an answer on June 13, 2026 at 7:41 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code: #include <string> #include <vector> #include <iostream> using namespace std; class
When trying to compile this code: #include <iostream> #include <vector> using namespace std; class
Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Look at this code please: #include <iostream> using namespace std; class Ratio { public:
Consider this piece of code: #include <vector> #include <iostream> using namespace std; class Base
I've got this code: #include <iostream> #include <string> #include <vector> using namespace std; vector<string>
I'm having a problem with this code: #include <iostream> using namespace std; class A
Why this code gives error: #include<iostream> #include<vector> using namespace std; int main() { char
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
This very simple code: #include <iostream> using namespace std; void exec(char* option) { cout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.