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
|
Occasionaly, the linker will complain with a message like the following:
hi(undefined reference to vtable)
hi(vtable: undefined reference)
verb(
In function
`Derived::Derived[in-charge]()':
: undefined reference to `vtable for Derived'
)
This error is caused by the absence of the implementation of
a virtual function in a derived class, while the function is mentioned in the
derived class's interface.
Such a situation can easily be created:
itemization(
it() Construct a (complete) base class defining a virtual member function;
it() Construct a Derived class which mentions the virtual function in its
interface;
it() The Derived class's virtual function, overriding the base class's
function having the same name, is not implemented. Of course, the compiler
doesn't know that the derived class's function is not implemented and will,
when asked, generate code to create a derived class object;
it() However, the linker is unable to find the derived class's virtual
member function. Therefore, it is unable to construct the derived class's
vtable;
it() The linker complains with the message:
verb(
undefined reference to `vtable for Derived'
)
)
Here is an example producing the error:
verbinclude(polymorphism/examples/vtable.cc)
It's of course easy to correct the error: implement the derived class's
missing virtual member function.
|