Re: undefined reference to vtable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



eric <cneric12lin0@xxxxxxxxx> writes:

> actually that book even did not specially define Superhero::walk(),
> that is I add by myself to escape my compile's error
> (is that right? or book's is right?)

I see you did not define Superhero::eat(), however.  You must
define all the virtual functions of class Superhero, even if the
program never calls them.  http://gcc.gnu.org/faq.html#vtables

http://www.codesourcery.com/public/cxx-abi/abi.html#vague-vtable
and http://gcc.gnu.org/onlinedocs/gcc-4.5.2/gcc/Vague-Linkage.html
explain that GCC emits the vtable in the same translation unit
as the first non-inline, non-pure virtual function of the class.
In class Superhero, that function is Superhero::eat().  Because
you did not define Superhero::eat(), GCC did not emit the vtable,
and the linker then failed because the vtable was missing.

If you add a definition for Superhero::eat(), you get different
errors from the linker:

/tmp/ccuVi68j.o: In function `main':
Example8-11.eat.cpp:(.text+0x58): undefined reference to `Superhero::~Superhero()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x18): undefined reference to `Superhero::sleep()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x28): undefined reference to `Superhero::jump()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x38): undefined reference to `Superhero::up()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x40): undefined reference to `Superhero::down()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x48): undefined reference to `Superhero::~Superhero()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x50): undefined reference to `Superhero::~Superhero()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x70): undefined reference to `non-virtual thunk to Superhero::up()'
/tmp/ccuVi68j.o:(.rodata._ZTV9Superhero[vtable for Superhero]+0x78): undefined reference to `non-virtual thunk to Superhero::down()'
collect2: ld returned 1 exit status

Now it tells you which functions are missing.  So, when you get a
linker error about an undefined reference to a vtable, do check
that you have defined all the declared functions in that class.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux