PDA

View Full Version : undefined reference to 'vtable ...'



GUIman
8th March 2011, 22:27
I don't understand this error:

Undefined reference to 'vtable for Spaceship' Spaceship.cpp 9
Undefined reference to 'vtable for Spaceship' Spaceship.cpp 9
collect2: ld returned 1 exit status

spaceship.h:

#ifndef SPACESHIP_H
#define SPACESHIP_H

#include <QGraphicsItem>
#include <QObject>

class Spaceship : public QGraphicsItem
{

public:
Spaceship();

QRectF boundingRect() const;
QPainterPath shape() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

protected:
void advance(int step);

private:
qreal angle;
qreal speed;
QColor color;
};

#endif // SPACESHIP_H

spaceship.cpp:

#include "spaceship.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <QObject>
#include <math.h>

Spaceship::Spaceship()
{
setRotation(0);
}

void Spaceship::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
//Draw the Spaceship
painter->setBrush(Qt::black);
painter->drawRect(10, 10, 20, 20);
}

void Spaceship::advance(int step)
{
if (!step)
return;

angle = 0;
speed += 1;
setPos(mapToParent(0, -(3 + speed) *3));
}

main.cpp:

#include "spaceship.h"
#include <QtGui>
#include <math.h>
#include <QObject>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
QGraphicsScene scene;
scene.setSceneRect(-300, -300, 600, 600);
scene.setItemIndexMethod(QGraphicsScene::NoIndex);

Spaceship *ship = new Spaceship;
ship->setPos(0,0);
scene.addItem(ship);

QGraphicsView view(&scene);
view.setRenderHint(QPainter::Antialiasing);

view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsVie w, "Spaceship"));
view.resize(400, 300);
view.show();

QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()),&scene, SLOT(advance()));
timer.start(1000 / 33);

return app.exec();
}

I was trying to modify the colliding mice example to just show a block move upward.

Thank you very much

Zlatomir
8th March 2011, 22:36
Have you defined this two member functions: QRectF boundingRect() const;, QPainterPath shape() const;?

SixDegrees
8th March 2011, 22:46
Also, make sure you've added both the header and the implementation files to your .pro file.

schnitzel
8th March 2011, 23:06
try cleaning the project, remove debug/release folders, remove the makefiles and the .pro.user file, then open the project again and rebuild.

ChrisW67
8th March 2011, 23:48
You need to provide the implementations for two virtual functions (one pure) you declare:


QRectF boundingRect() const;
QPainterPath shape() const;

and the error goes away.

GUIman
10th March 2011, 01:38
Thank you very much... I am an idiot