Undefined symbols: vtable for Foo

R

r

I compile this on a Max OSX with
GCC version 1151, based on gcc version 3.1 20020420

g++ foo.cpp
ld: Undefined symbols: <-- ERROR
vtable for Foo

Any idea why?
-r
**************** contents of foo.cpp **********

class Foo {

protected:
void setit(int);

public:
virtual void bar(); // only a subclass implements
};


class Bird : public Foo {

public:
void bar();
};

// ################

int a;

void Foo::setit(int i) {
a = i;
}

void Bird::bar() {
Foo::setit(8);
}

int main(int argc, char **argv) {
Bird *b = new Bird();
b->bar();
}

// end
 
G

Gianni Mariani

r said:
I compile this on a Max OSX with
GCC version 1151, based on gcc version 3.1 20020420

g++ foo.cpp
ld: Undefined symbols: <-- ERROR
vtable for Foo

Any idea why?
-r
**************** contents of foo.cpp **********

class Foo {

protected:
void setit(int);

public:
virtual void bar(); // only a subclass implements

try:
virtual void bar() = 0;


virtual void bar() indicates that the compiler expects there to be an
implementation.
 
R

Rob Williscroft

r wrote in
I compile this on a Max OSX with

GCC version 1151, based on gcc version 3.1 20020420



g++ foo.cpp
ld: Undefined symbols: <-- ERROR
vtable for Foo

[snip]

class Foo {

protected:
void setit(int);

public:

change this:
virtual void bar(); // only a subclass implements

To:

virtual void bar() = 0;
};


class Bird : public Foo {

public:
void bar();
};

// ################


int a;
[snip]

Adding the = 0; to the declaration of bar() above makes bar()
a pure virtual and Foo an abstract class. In effect telling
the compiler you want to derive from Foo but NOT create any
instances of Foo. The compiler can then omit creating a vtable
for Foo and your linker error will go away.

Alternatively provide an defenition for Foo::bar()

voif Foo::bar()
{
}

Do it this way if you want to create instances of Foo.

HTH

Rob.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top