Undefined symbols: vtable for Foo

Discussion in 'C++' started by r, Aug 2, 2003.

  1. r

    r Guest

    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
     
    r, Aug 2, 2003
    #1
    1. Advertising

  2. 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
     
    Gianni Mariani, Aug 2, 2003
    #2
    1. Advertising

  3. r wrote in news::

    > 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/
     
    Rob Williscroft, Aug 2, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Heiserer
    Replies:
    1
    Views:
    2,912
    Artie Gold
    Aug 1, 2003
  2. Replies:
    3
    Views:
    384
    John Roth
    Jul 29, 2005
  3. Replies:
    6
    Views:
    370
  4. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    396
    JaredGrubb
    Sep 24, 2008
  5. Replies:
    4
    Views:
    146
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page