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

I can't figure out what the issue is here. It occurs on line 60(and 61).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #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;
}


Thanks for any help!
Hi,

Both the Dog and Cat classes need to have a constructor that calls the Pet constructor.

If the base class has just one pure virtual function that is not overridden, then the derived class is also abstract.

When over riding a function use the override keyword.
Line60: The class Dog does not have an appropriate constructor.

You might do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Dog : public Pet
{
public:
  Dog(const string &name) : Pet{"dog", name}
  {
  }
...
};

...
int main()
{
    Dog spot{"Spot"}; // The type is sent to Pet automatically
...


As the TheIdeasMan stated: It is preferable to use the keyword override:
1
2
3
4
5
6
7
class Dog : public Pet
{
public:
    void whoAmI() const override;  // override the describe() function
    string speak() override;

};
Then you will see that speak() isn't correctly overwritten (missing const).

Topic archived. No new replies allowed.