943,677 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 155178
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 17th, 2008
1

Undefined reference to 'vtable for ...'

Expand Post »
I'm self studying from the book, C++ Primer Plus Fifth Edition, by Stephen Prata. The following relates to Chapter 13, Page 699, Programming Exercise #4. One task is to write the derived class method definitions based upon the given prototypes. The following are the said prototypes.

cpp Syntax (Toggle Plain Text)
  1. class Port
  2. {
  3. private:
  4. char *brand;
  5. char style[20]; // i.e. tawny, ruby, vintage
  6. int bottles;
  7. public:
  8. Port(const char *br = "none", const char *st = "none", int b = 0);
  9. Port(const Port &p); // copy constructor
  10. virtual ~Port() { delete [] brand;}
  11. Port & operator=(const Port &p);
  12. Port & operator+=(int b);
  13. Port & operator-=(int b);
  14. int BottleCount() const {return bottles;}
  15. virtual void Show() const;
  16. friend ostream &operator<<(ostream &os, const Port &p);
  17. };
  18.  
  19. class VintagePort : public Port
  20. {
  21. private:
  22. char * nickname; // i.e. The Noble, or Old Velvet, etc.
  23. int year; // vintage year
  24. public:
  25. VintagePort();
  26. VintagePort(const char *br, int b, const char *nn, int y);
  27. VintagePort(const VintagePort &vp);
  28. ~VintagePort() {delete [] nickname;}
  29. void Show() const;
  30. friend ostream & operator<<(ostream &os, const VintagePort & vp);
  31. };

You will note that the base class destructor is virtual. That the derived class destructor is implemented inline.

I was creating the derived class method definitions in a cpp file and couldn't get the project to progressively compile correctly. At my first method definition, a default constructor, the compiler kept spitting out this error message:
Quote ...
Chapter-13\pe-13-04\port.cpp|74|undefined reference to `vtable for VintagePort'
The line number was pointing to my derived class constructor definition. I did some googling around and came across this workaround. I removed the inline effect of the derived class destructor, made that into a method definition in the cpp file and presto, compilation succeeded.

Am I to assume that the example in the book, as regards the inline feature of a derived class destructor, is in error. Are there any other explanations.
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Mar 17th, 2008
1

Re: Undefined reference to 'vtable for ...'

> I was creating the derived class method definitions in a cpp file
> and couldn't get the project to progressively compile correctly.
> At my first method definition, a default constructor,
> the compiler kept spitting out this error message:

this is perhaps the most obscure error message that gcc (actually the linker) spits out, but the reason is simple:

the compiler has to put the (one and only one) vtable for a class into some object file or the other. it puts it into the object file for the translation unit where the definition of the first non-pure-virtual out-of-line virtual member function is present. if there is no such definition, you get this rather unhelpful linker error message.

you would be able to progressively compile the code even if a non-pure virtual function is not defined, but to be able to link without errors, every such function must have a definition (at least a stub). and to prevent the non-creation of a v-table by the compiler, at least one of the non-pure virtual functions will have to be defined out-of-line.

gcc has a faq about this: http://gcc.gnu.org/faq.html#vtables


> Am I to assume that the example in the book, as regards the inline feature
> of a derived class destructor, is in error.
i think it is not a particularly good book, but in this case neither the book nor the compiler is in error.
if you define the destructor inline, and void VintagePort::Show() const out-of-line, the error will go away. there has to be at least one out-of-line definition of a non-pure-virtual function.

note: the microsoft compiler (as well as several other compilers) does not require this; it instantiates a v-table with internal linkage in *every* translation unit in which such a header (all non-pure virtual functions are inline) is included. this violates the c++ one definition rule for v-tables, but as always, folks at redmond tend to value pragmatism highly. and they do give you __declspec(novtable) to suppress the proliferation of v-tables.
Last edited by vijayan121; Mar 17th, 2008 at 11:00 am.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Mar 17th, 2008
0

Re: Undefined reference to 'vtable for ...'

Thank you very much, this has certainly cleared things up. In a way, I'm glad I came across this problem, hopefully it won't catch me out again, when progressively compiling.
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Feb 2nd, 2009
1

Re: Undefined reference to 'vtable for ...'

Hi ... I hope this helps someone out there....

I'm using netbeans with the C++ plugin in linux. I got the vtable error, and after a long time I figured out the solution..

basically the class I was trying to use was not added into the project. The class files (.cpp, .h) were in the project folder, and i was editing the files, etc... but they just wouldn't compile because they weren't added into the project.

so just right click the project, select "add existing items" and choose the file and add the files to the project... now it should compile, fingers crossed
Reputation Points: 11
Solved Threads: 2
Newbie Poster
ronjustincase is offline Offline
12 posts
since Dec 2008
Feb 18th, 2009
0

Re: Undefined reference to 'vtable for ...'

I have also seen this with GCC 4 when a virtual destructor is missing a body
eg:
virtual ~MyClass(void);
should be:
virtual ~MyClass(void){};
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Procrastes is offline Offline
1 posts
since Feb 2009
Jul 16th, 2009
0

Re: Undefined reference to 'vtable for ...'

Click to Expand / Collapse  Quote originally posted by Procrastes ...
I have also seen this with GCC 4 when a virtual destructor is missing a body
eg:
virtual ~MyClass(void);
should be:
virtual ~MyClass(void){};
Also if you declare a virtual constructor but forget the '~' in its definition, e.g.:
virtual ~A(); // declaration in .h file
A::A() {} // .cpp implementation missing ~ in function name
Reputation Points: 0
Solved Threads: 1
Light Poster
dorkwad is offline Offline
34 posts
since Jun 2009
Dec 19th, 2009
0

No...

Click to Expand / Collapse  Quote originally posted by dorkwad ...
Also if you declare a virtual constructor but forget the '~' in its definition, e.g.:
virtual ~A(); // declaration in .h file
A::A() {} // .cpp implementation missing ~ in function name
That doesn't make sense... Removing '~' makes your function a 'Constructor' instead of a 'Destructor'...

The only difference between the .H version and the .CXX version is that you don't put 'virtual' in the .CXX file...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SsJ_Vasto is offline Offline
1 posts
since Dec 2009
Feb 4th, 2010
0

Addition

I'm self studying from the book, C++ Primer Plus Fifth Edition, by Stephen Prata. The following relates to Chapter 13, Page 699, Programming Exercise #4. One task is to write the derived class method definitions based upon the given prototypes. The following are the said prototypes.

cpp Syntax (Toggle Plain Text)
  1. class Port
  2. {
  3. private:
  4. char *brand;
  5. char style[20]; // i.e. tawny, ruby, vintage
  6. int bottles;
  7. public:
  8. Port(const char *br = "none", const char *st = "none", int b = 0);
  9. Port(const Port &p); // copy constructor
  10. virtual ~Port() { delete [] brand;}
  11. Port & operator=(const Port &p);
  12. Port & operator+=(int b);
  13. Port & operator-=(int b);
  14. int BottleCount() const {return bottles;}
  15. virtual void Show() const;
  16. friend ostream &operator<<(ostream &os, const Port &p);
  17. };
  18.  
  19. class VintagePort : public Port
  20. {
  21. private:
  22. char * nickname; // i.e. The Noble, or Old Velvet, etc.
  23. int year; // vintage year
  24. public:
  25. VintagePort();
  26. VintagePort(const char *br, int b, const char *nn, int y);
  27. VintagePort(const VintagePort &vp);
  28. ~VintagePort() {delete [] nickname;}
  29. void Show() const;
  30. friend ostream & operator<<(ostream &os, const VintagePort & vp);
  31. };

You will note that the base class destructor is virtual. That the derived class destructor is implemented inline.

I was creating the derived class method definitions in a cpp file and couldn't get the project to progressively compile correctly. At my first method definition, a default constructor, the compiler kept spitting out this error message:

The line number was pointing to my derived class constructor definition. I did some googling around and came across this workaround. I removed the inline effect of the derived class destructor, made that into a method definition in the cpp file and presto, compilation succeeded.

Am I to assume that the example in the book, as regards the inline feature of a derived class destructor, is in error. Are there any other explanations.
This error also arises in gcc if a class is inherited from a class in non included library but added include file.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dimbard is offline Offline
1 posts
since Feb 2010
Jun 3rd, 2010
0
Re: Undefined reference to 'vtable for ...'
Thanks. Procrastes's answer helped. Virtual destructor was missing a body and the error was gone when I fixed it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
athena_1 is offline Offline
1 posts
since Jun 2010
Jun 11th, 2010
0

virtual ~A();

Dont put virtual if you dont need.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jecaestevez is offline Offline
1 posts
since Jun 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Nested classes help?
Next Thread in C++ Forum Timeline: Optimization question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC