473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

undefined reference to `vtable for Base'

Hello all,

class Base {
public:
virtual void f(); };

class Derived : public Base{
private:
int i; };

int main() {
Derived arrayOfDerived[10]; }

/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccwM5eGM.o(.rdata$_ZTV7Derived[vtabl
e for Derived]+0x8):wrong.cpp: undefined reference to `Base::f()'
/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccwM5eGM.o(.text$_ZN4BaseC2Ev[Base::
Base[not-in-charge]()]+0x8):wrong.cpp: undefined reference to `vtable for
Base'
collect2: ld returned 1 exit status

Why doesnt this piece of code compile?

TIA

Aug 10 '05 #1
9 9619
jimjim wrote:
class Base {
public:
virtual void f(); };
The rule is simple: a virtual function shall be defined or declared pure
or both. Yours is not pure. Hence it _must_ be defined.
class Derived : public Base{
private:
int i; };

int main() {
Derived arrayOfDerived[10]; }

/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccwM5eGM.o(.rdata$_ZTV7Derived[vtabl
e for Derived]+0x8):wrong.cpp: undefined reference to `Base::f()'
/cygdrive/c/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccwM5eGM.o(.text$_ZN4BaseC2Ev[Base::
Base[not-in-charge]()]+0x8):wrong.cpp: undefined reference to `vtable for
Base'
collect2: ld returned 1 exit status

Why doesnt this piece of code compile?


But it does. The program fails to link, not compile.

The compiler assumes that the function is defined somewhere else and
lets the linker resolve it. The linker can't find any definition for
it and complains. What you have here is a simple violation of the
rules of the language, that's all.

V
Aug 10 '05 #2
you have forgotten to define the function f(). plz define it either in
base classs or in the derived class.

Aug 11 '05 #3
> The rule is simple: a virtual function shall be defined or declared pure
or both. Yours is not pure. Hence it _must_ be defined.

thx for the reply
int main() {
Derived arrayOfDerived[10]; }

if I make the following change the program links ! Why is this?

int main() {
Derived arrayOfDerived(); }
Aug 11 '05 #4
<ra************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
you have forgotten to define the function f(). plz define it either in
base classs or in the derived class.

thx for the reply
Aug 11 '05 #5
Le jeudi 11 août 2005 à 13:06, jimjim a écrit dans comp.lang.c++*:
int main() {
Derived arrayOfDerived[10]; }

if I make the following change the program links ! Why is this?

int main() {
Derived arrayOfDerived(); }


Because there is no object instantiated now.
You just declared a parameter-less function returning a Derived.

--
___________ 11/08/2005 14:33:46
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
Aug 11 '05 #6
> Because there is no object instantiated now.
You just declared a parameter-less function returning a Derived.

I can only lough with my question :-)
Aug 11 '05 #7
> Because there is no object instantiated now.
You just declared a parameter-less function returning a Derived.

didnt really know that I can declare a function in the body of another
function! What is the benefit of this?

TIA
Aug 11 '05 #8
jimjim wrote:
Because there is no object instantiated now.
You just declared a parameter-less function returning a Derived.


didnt really know that I can declare a function in the body of another
function! What is the benefit of this?


Limit the scope in which that name appears. You can declare a function
at any level. The deeper the scope nesting the less pollution to the
outside scopes.

V
Aug 11 '05 #9
thx
Aug 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel Heiserer | last post by:
Hi, I got the following compiling/linking error, but I have no clue what it is about. Can anybody help me? thanks, daniel
2
by: Quansheng Liang | last post by:
Hello, I struggled with the problem of "undefined reference to `vtable ...`" while migrating a project from windows to linux. After searching the google I removed all the inline functions and now...
3
by: bp | last post by:
Hi, Could someone to tell me what I'm doing wrong? I'm using gcc version 3.3.4 and ld version 2.15.90.0.3 20040415 the linker message is: .../obj/classOpenAreaForm.o(.text+0x211): In...
7
by: Karl Ebener | last post by:
Hi! I have created a program using several classes with inheritage. When I compile and link, I get the following error: :$ g++ service2.cpp Service.cpp Application.cpp Message.cpp...
9
by: kish_nand | last post by:
Could someone please explain me the concept behind virtual functions and vtables. I am little confused about this. Please refer to the following code and tell me how many virtual tables would be...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
5
by: pavan734 | last post by:
Hi, Iam facing problem with undefined reference linking errors. To explain in more detail.. I have a class called TOP. In its constructor I have instantiated many other class objects using...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
2
by: boyphp | last post by:
I'm getting the following undefined reference errors: foo_test.o(.gnu.linkonce.t._ZN7CFooD1Ev+0x18): In function `CFoo::~CFoo()': : undefined reference to `vtable for CFoo'...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.