inheritance compiler error?

hi I'm using inheritance in my code yet I get an error everything looks fine to me but it's not fine to the compiler the error I'm getting is undefined reference to 'vtable for cat yet this only happens when I try to declare my class cat in main.any ideas yo why this is happening?

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
  
#include <iostream>
#include <vector>

using namespace std;

class animal{


public:

    virtual void makeSound(){};
    void Iam(){


         cout << "I m just an animal" << endl;

    }

};

class cat: public animal{


 public:

     void makeSound();


};


int main()
{

    cat c;



}

Last edited on
You have forgot to provide a definition of the cat::makeSound() function.
does anybody else know why the function makeSound won't get called in the for loop it gives me an error a very very long one,yet pretty much the same code will work for the author in the book

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
67
68
69
70
71
72
73
74
75
76



#include <iostream>
#include <vector>

using namespace std;

class animal{


public:

    virtual void makeSound(){};
    void Iam(){


         cout << "I m just an animal" << endl;

    }

};

class cat: public animal{


 public:

     void makeSound(){

        cout << "meow" << endl;

     };


};


class dog: public animal{


   public:

       void makeSound(){

           cout << "ruff ruff" << endl;


       }

};


int main()
{

    vector<animal*> animals;
    vector<animal*>:: iterator itr;

    animals.push_back(new cat);
    animals.push_back(new dog);

    for(itr = animals.begin();itr != animals.end();itr++){


         cout << (*itr)->makeSound() << endl; // error here

    }



}



The makeSound() function doesn't return anything (the return type is void) so trying to print the returned value doesn't make sense.

Either call it without using cout ...

1
2
3
for(itr = animals.begin(); itr != animals.end(); itr++) {
	(*itr)->makeSound();
}


... or change the return type of the function and return a string instead of printing it (Don't forget to update all three functions).

1
2
3
std::string makeSound() {
	return "meow";
};
Line 14,29,44: makeSound() is type void.

Line 66: You can't cout a void. Change line 66 to a simple function call.
 
    (*itr)->makeSound(); 
Thanks petter that makes sense I finally figured it out after all it was frustraiting me haha

thanks again =)
Topic archived. No new replies allowed.