Discussion:
undefined reference to vtable
eric
2011-07-15 23:05:46 UTC
Permalink
Dear Advanced C/g++ programers:

a simple program about Creating an interface with an Abstract Base
Class. from book (C++ cookbook) page 308, 309. Example 8-11. using a
pure interface.
----------------------
// Example 8-11 Using a pure interface
class Person {
public:
virtual void eat() = 0;
virtual void sleep() = 0;
virtual void walk() = 0;
virtual void jump() = 0;
};

class IAirbone {
public:
virtual void fly() = 0;
virtual void up() = 0;
virtual void down() = 0;
};

class Superhero : public Person, // A superhero *is* a person
public IAirbone { // and flies
public:
virtual void eat();
virtual void sleep();
virtual void walk();
virtual void jump();
virtual void fly();
virtual void up();
virtual void down();
virtual ~Superhero();
};

void Superhero::walk() {
// ...
}

void Superhero::fly() {
// ...
}

// Implement all of the pure virtuals in Superhero's Superclasses...

int main() {

Superhero superman;
superman.walk(); // Superman can walk like a person
superman.fly(); // or fly like a bird
}
---------------------------------------------------
my g++ 4.5.2 (on linux2.6.35-25) response by
-------------------------------------
***@eric-laptop:~/cppcookbook/ch8$ g++ Example8-11.cpp
/tmp/ccT3nO5t.o: In function `main':
Example8-11.cpp:(.text+0x47): undefined reference to
`Superhero::~Superhero()'
/tmp/ccT3nO5t.o: In function `Superhero::Superhero()':
Example8-11.cpp:(.text._ZN9SuperheroC2Ev[_ZN9SuperheroC5Ev]+0x24):
undefined reference to `vtable for Superhero'
Example8-11.cpp:(.text._ZN9SuperheroC2Ev[_ZN9SuperheroC5Ev]+0x2e):
undefined reference to `vtable for Superhero'
collect2: ld returned 1 exit status
--------------------------------------------------
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?)
you can download the source code of that book's example and test by
yourself
http://examples.oreilly.com/9780596007614/
according to book, thses code are compile good in visual c++ 7.1 on
window xp
thanks your help a lot in advance, Eric
Philip Herron
2011-07-16 03:53:30 UTC
Permalink
I think your questions might get a better response from something like
comp.lang.c++ or any of the c++ channels on irc like irc.freenode.org
##c++
Post by eric
a simple program about Creating an interface with an Abstract Base
Class. from book (C++ cookbook) page 308, 309. Example 8-11. using a
pure interface.
----------------------
// Example 8-11 Using a pure interface
class Person {
virtual void eat() = 0;
virtual void sleep() = 0;
virtual void walk() = 0;
virtual void jump() = 0;
};
class IAirbone {
virtual void fly() = 0;
virtual void up() = 0;
virtual void down() = 0;
};
class Superhero : public Person, // A superhero *is* a person
public IAirbone { // and flies
virtual void eat();
virtual void sleep();
virtual void walk();
virtual void jump();
virtual void fly();
virtual void up();
virtual void down();
virtual ~Superhero();
};
void Superhero::walk() {
// ...
}
void Superhero::fly() {
// ...
}
// Implement all of the pure virtuals in Superhero's Superclasses...
int main() {
Superhero superman;
superman.walk(); // Superman can walk like a person
superman.fly(); // or fly like a bird
}
---------------------------------------------------
my g++ 4.5.2 (on linux2.6.35-25) response by
-------------------------------------
Example8-11.cpp:(.text+0x47): undefined reference to
`Superhero::~Superhero()'
undefined reference to `vtable for Superhero'
undefined reference to `vtable for Superhero'
collect2: ld returned 1 exit status
--------------------------------------------------
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?)
you can download the source code of that book's example and test by
yourself
http://examples.oreilly.com/9780596007614/
according to book, thses code are compile good in visual c++ 7.1 on
window xp
thanks your help a lot in advance, Eric
Kevin P. Fleming
2011-07-16 12:40:14 UTC
Permalink
Post by Philip Herron
I think your questions might get a better response from something like
comp.lang.c++ or any of the c++ channels on irc like irc.freenode.org
##c++
He's already posting these on comp.lang.c++ as well, but continues to
post them here anyway.
--
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
Jabber: ***@digium.com | SIP: ***@digium.com | Skype: kpfleming
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
Check us out at www.digium.com & www.asterisk.org
Kalle Olavi Niemitalo
2011-07-16 18:20:16 UTC
Permalink
Post by eric
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.
Jonathan Wakely
2011-07-16 18:22:48 UTC
Permalink
Post by eric
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://gcc.gnu.org/ml/gcc-help/2011-07/msg00143.html

Loading...