[Linker error] undefined reference to vtable

Discussion in 'C++' started by Mark, Mar 13, 2006.

  1. Mark

    Mark Guest

    Hi,
    I'm trying to write some classes that kind of manage themselves. A
    linked list, that links different types of objects. Here's the code...

    object.h:
    ---
    class Object
    {
    int alt;

    public:
    Object *next;
    Object();
    virtual void draw() = 0;
    };

    class Projectile : public Object
    {
    double x, y;
    double vel, dir;

    public:
    Projectile(double,double,double,double);
    void draw();
    };

    object.cpp
    ---
    #include "object.h"

    Object *list = NULL;

    Object::Object()
    {
    next = list;
    list = this;
    printf("object constructor got called\n");
    }

    Projectile::projectile(double x, double y, double vel, double dir)
    : x(x), y(y), vel(vel), dir(dir)
    {
    printf("projectile constructor got called\n");
    }

    void Projectile::draw()
    {
    drawDisk((int)x,(int)y,4,ColorRGB(255,0,0));
    }


    This linker error appears 4 times,
    [Linker error] undefined reference to `vtable for Object'
    And I'm not sure how to fix it. Any help?
    Mark, Mar 13, 2006
    #1
    1. Advertising

  2. Mark wrote:
    > I'm trying to write some classes that kind of manage themselves. A
    > linked list, that links different types of objects. Here's the
    > code...
    >
    > object.h:
    > ---
    > class Object
    > {
    > int alt;
    >
    > public:
    > Object *next;
    > Object();
    > virtual void draw() = 0;


    If you have pure virtual functions in your class, it's a good
    indication that you probably need a virtual destructor, even if
    it doesn't have anything to do.

    > };
    >
    > class Projectile : public Object
    > {
    > double x, y;
    > double vel, dir;
    >
    > public:
    > Projectile(double,double,double,double);
    > void draw();
    > };
    >
    > object.cpp
    > ---
    > #include "object.h"
    >
    > Object *list = NULL;
    >
    > Object::Object()
    > {
    > next = list;
    > list = this;
    > printf("object constructor got called\n");
    > }
    >
    > Projectile::projectile(double x, double y, double vel, double dir)
    >> x(x), y(y), vel(vel), dir(dir)

    > {
    > printf("projectile constructor got called\n");
    > }
    >
    > void Projectile::draw()
    > {
    > drawDisk((int)x,(int)y,4,ColorRGB(255,0,0));
    > }
    >
    >
    > This linker error appears 4 times,
    > [Linker error] undefined reference to `vtable for Object'
    > And I'm not sure how to fix it. Any help?


    You don't show how your classes are used, we can't help you. Please
    read the FAQ, especially # 5.8.

    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Mar 13, 2006
    #2
    1. Advertising

  3. Mark

    Mark Guest

    Re: undefined reference to vtable

    well, i hadnt used my classes yet, because i couldnt get it to compile
    :p

    intended usage is like so:

    Object *obj = list;
    while( obj != NULL )
    {
    obj->draw();
    obj = obj->next;
    }

    anyway, adding
    ~Object();
    to class Object fixed the problem. thanks!
    not really sure how virtual destructors might work... but oh well. long
    as it compiles and does what i ask...
    Mark, Mar 13, 2006
    #3
  4. Mark

    Githlar Guest

    Why is there a line that states:
    virtual void draw() = 0;

    To my knowledge this is an invalid line.
    Githlar, Mar 13, 2006
    #4
  5. Mark

    Githlar Guest

    OK, I was wrong. =0 = pure virtual. I forgot about that, lol.
    Githlar, Mar 13, 2006
    #5
    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,891
    Artie Gold
    Aug 1, 2003
  2. Quansheng Liang
    Replies:
    2
    Views:
    546
    Quansheng Liang
    May 25, 2004
  3. bp
    Replies:
    3
    Views:
    20,700
    Rolf Magnus
    Aug 20, 2004
  4. alessandro
    Replies:
    9
    Views:
    6,033
    John Harrison
    Sep 14, 2004
  5. Karl Ebener

    Undefined reference to vtable

    Karl Ebener, Nov 19, 2004, in forum: C++
    Replies:
    6
    Views:
    49,440
    Karl Ebener
    Nov 19, 2004
Loading...

Share This Page