LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-15-2011, 05:42 PM   #1
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Rep: Reputation: 1
undefined reference, but I think I already defined them


Dear Advanced c/g++ programers:

I tested 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
 
Old 07-15-2011, 06:15 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I assume the following comment means that you implemented all of the remaining virtual methods:
Code:
// Implement all of the pure virtuals in Superhero's Superclasses...
Do not forget to declare your destructor as a 'virtual' method too; and of course, implement it. That's what this error is telling you:
Code:
Example8-11.cpp.text+0x47): undefined reference to `Superhero::~Superhero()'
 
Old 07-15-2011, 06:30 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You should also make Person::~Person() and IAirborne::~IAirborne() virtual with empty ({}) function bodies. This will give you proper behavior if you want to delete a Person* or an IAirborne*; they will always have a different underlying type if non-NULL, and making the destructors virtual will call the destructor of the most-derived class.
Kevin Barry
 
Old 07-15-2011, 06:45 PM   #4
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Original Poster
Rep: Reputation: 1
thanks your hint
but I am not smart enough to figure out how to fix by your English only explanation.
would you please show out the code?
 
Old 07-15-2011, 07:02 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
"Bad" example:
Code:
#include <iostream>

struct base
{
  virtual void something() = 0;
};

struct derived : public base
{
  void something() { std::cout << "something\n"; }
  ~derived() { std::cout << "good\n"; }
};

int main()
{
  base *pointer = new derived;
  pointer->something();
  delete pointer;
}
"Good" example:
Code:
#include <iostream>

struct base
{
  virtual void something() = 0;
  virtual ~base() {}
};

struct derived : public base
{
  void something() { std::cout << "something\n"; }
  ~derived() { std::cout << "good\n"; }
};

int main()
{
  base *pointer = new derived;
  pointer->something();
  delete pointer;
}
Run both and see what the difference is.
Kevin Barry

Last edited by ta0kira; 07-15-2011 at 07:04 PM.
 
Old 07-15-2011, 10:35 PM   #6
fsshl
Member
 
Registered: Jan 2002
Distribution: Ubuntu10.04
Posts: 49

Original Poster
Rep: Reputation: 1
Do not forget to declare your destructor as a 'virtual' method too; and of course, implement it. That's what this error is telling you:

I won't forget until someone tell me how to fix it by real code
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another undefined reference CollieJim Linux - General 2 12-17-2010 11:08 PM
Undefined reference, why? george_mercury Programming 4 05-07-2009 12:15 AM
undefined reference? Sharky01252 Programming 3 11-07-2006 11:36 AM
undefined reference vkmgeek Programming 1 05-11-2006 06:37 AM
Python: making a reference to a function not yet defined JRR883 Programming 1 02-08-2006 11:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration