[Linker error] undefined reference to vtable

M

Mark

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?
 
V

Victor Bazarov

Mark said:
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
 
M

Mark

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...
 
G

Githlar

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

To my knowledge this is an invalid line.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top