-1

When I try to run my program it prints out

"error: no matching function for call to Dog::Dog(const char [4], const char [5])".

This occurs at line 60 and 61. Is it reading the arguments as a C-String? I should still be able to pass it into the constructor, can't I?

#include <iostream> #include <string> using namespace std; #include <string> class Pet { protected: string type; string name; public: Pet(const string& arg1, const string& arg2); virtual void whoAmI() const; virtual string speak() const = 0; }; Pet::Pet(const string& arg1, const string& arg2): type(arg1), name(arg2) {} void Pet::whoAmI() const { cout << "I am an excellent " << type << " and you may refer to me as " << name << endl; } class Dog : public Pet { public: void whoAmI() const; // override the describe() function string speak(); }; string Dog::speak() { return "Arf!"; } class Cat : public Pet { string speak(); // Do not override the whoAmI() function }; string Cat::speak() { return "Meow!"; } ostream& operator<<(ostream& out, const Pet& p) { p.whoAmI(); out << "I say " << p.speak(); return out; } int main() { Dog spot("dog","Spot"); Cat socks("cat","Socks"); Pet* ptr = &spot; cout << *ptr << endl; ptr = &socks; cout << *ptr << endl; } 

Any help is greatly appreciated!

3
  • Hint: does C++ [insert version here] "inherit constructors"? Commented Mar 22, 2018 at 3:16
  • 1
    There is more wrong with the code than the lack of constructors for Dog and Cat. Voted to reopen. Commented Mar 22, 2018 at 3:29
  • ideone.com/XptrrQ should do Commented Mar 22, 2018 at 8:07

1 Answer 1

0

Pet has constructor taking 2 string, not Dog.

You might use Base constructor thanks to using (since C++11):

class Dog : public Pet { public: using Pet::Pet; void whoAmI() const; // override the describe() function string speak(); }; 

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

There is more stuff wrong than the "constructor inheritance".
@AdrianoMartins: Indeed, there is also wrong override because of constness.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.