undefined reference to `vtable for Base'

J

jimjim

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
 
V

Victor Bazarov

jimjim said:
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
 
R

ravinderthakur

you have forgotten to define the function f(). plz define it either in
base classs or in the derived class.
 
S

Serge Paccalin

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
 
J

jimjim

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
 
V

Victor Bazarov

jimjim said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top