Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7849 > unrolled thread
| Started by | eric <cneric12lin0@gmail.com> |
|---|---|
| First post | 2011-07-15 16:05 -0700 |
| Last post | 2011-07-15 20:49 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.c++
undefined reference to vtable eric <cneric12lin0@gmail.com> - 2011-07-15 16:05 -0700
Re: undefined reference to vtable Ian Collins <ian-news@hotmail.com> - 2011-07-16 11:35 +1200
Re: undefined reference to vtable Arvind <bharti.arvind@gmail.com> - 2011-07-15 20:49 -0700
| From | eric <cneric12lin0@gmail.com> |
|---|---|
| Date | 2011-07-15 16:05 -0700 |
| Subject | undefined reference to vtable |
| Message-ID | <7609b85f-04be-42c1-8aae-5daba6bdd1bc@f39g2000prb.googlegroups.com> |
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@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
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-07-16 11:35 +1200 |
| Message-ID | <98c18mFv3qU5@mid.individual.net> |
| In reply to | #7849 |
On 07/16/11 11:05 AM, eric wrote:
> 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();
None of the above need the "virtual" key word.
> virtual ~Superhero();
Why have you given this a virtual destructor?
<snip>
> --------------------------------------------------
> 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?)
They must be defined.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Arvind <bharti.arvind@gmail.com> |
|---|---|
| Date | 2011-07-15 20:49 -0700 |
| Message-ID | <b8e28289-c485-440e-bb21-8277e77f1fb2@q29g2000prj.googlegroups.com> |
| In reply to | #7849 |
On Jul 16, 4:05 am, eric <cneric12l...@gmail.com> wrote:
> 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@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
> yourselfhttp://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
Just look at the error "Example8-11.cpp:(.text+0x47): undefined
reference to
> `Superhero::~Superhero()'".
Fix it. Define the destructor for Superhero because you have declared
this in Superhero class.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web