By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Undefined symbols: vtable for Foo

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
r wrote:
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.

};
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


Jul 19 '05 #2

P: n/a
r wrote in news:38*************************@posting.google.co m:
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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.