0

I made simple game to shot rectangle(class Bullet) from other rectangle(class MyRect). I have error undefined reference to 'vtable for bullet'. I have written that is connected with Q_OBJECT and moc.I can't resolve this problem.

Bullet.h

#ifndef BULLET_H
#define BULLET_H
#ifndef Q_MOC_RUN


#include <QGraphicsRectItem>
#include <QObject>



class Bullet: public QObject,public QGraphicsRectItem{
    Q_OBJECT
public:
    Bullet();
public slots:
    void move();
};

#endif
#endif // BULLET_H

MyRect.h

#ifndef MYRECT_H
#define MYRECT_H

#include <QGraphicsRectItem>

class MyRect: public QGraphicsRectItem{
public:
    void keyPressEvent(QKeyEvent* event);
};

#endif // MYRECT_H

Bullet.cpp

#include "Bullet.h"
#include <QTimer>

Bullet::Bullet(){

    setRect(0,0,10,50);   
    QTimer * timer = new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(move()));

    timer->start(50);
}

void Bullet::move(){

    this->setPos(x(),y()-10);
}

main.cpp

#include <QApplication>
#include <QGraphicsScene>
#include "MyRect.h"
#include <QGraphicsView>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGraphicsScene * scene = new QGraphicsScene();

    MyRect * rect = new MyRect();
    rect->setRect(0,0,100,100);

    rect->setFlag(QGraphicsItem::ItemIsFocusable);
    rect->setFocus();

    scene->addItem(rect);
    QGraphicsView * view = new QGraphicsView(scene);

    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    view->show();
    return a.exec();
}

MyRect.cpp

#include "MyRect.h"
#include<QKeyEvent>
#include <QGraphicsScene>
#include "Bullet.h"
#include<QDebug>

void MyRect::keyPressEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Left)
    {
        setPos(x()-10,y());
    }
    else if(event->key() == Qt::Key_Right)
    {
        setPos(x()+10,y());
    }
    else if(event->key() == Qt::Key_Up)
    {
        setPos(x(),y()-10);
    }
    else if(event->key() == Qt::Key_Down)
    {
        setPos(x(), y()+10);
    }
    else if(event->key() == Qt::Key_Space)
    {
        Bullet * bullet = new Bullet();
        bullet->setPos(x(), y());
        scene()->addItem(bullet);
    }

}

I have written some similar error i Qt, but still i can't resolve my.Thank you very much for your attention.

thibsc
  • 3,747
  • 2
  • 18
  • 38
Paweł
  • 1
  • 1
  • After a `qmake` you still have the error ? – thibsc May 03 '20 at 22:17
  • I didn't change qmake. I define q_moc_run. I don't have error, but my rectangle(bullet) doesn't change position – Paweł May 03 '20 at 22:38
  • I think, that bullet while not MyRect, becasue, i will dd picture (different to bullet and MyRect which shots) – Paweł May 03 '20 at 22:55
  • Don't know why you put the `Q_MOC_RUN` macro, remove it and test, works for me – thibsc May 03 '20 at 23:30
  • Most likely: make sure `Bullet.o` is included in the link command. The undefined reference to vtable message would trump any message about an undefined reference to `Bullet`'s constructor. – JaMiT May 04 '20 at 01:49
  • Does this answer your question? [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) – JaMiT May 04 '20 at 01:50
  • I chose option clean all and run in QT Creator and now it works – Paweł May 04 '20 at 10:46

1 Answers1

0

I got the same kind of error with this statement in a base class.

virtual void setInitiator(QPainter* painter, QImage* image);

Undefined reference to 'vtable for FractalObject'

FractalObject is my base class.

I derive classes which use the member function but there is no implementation in the base class. To fix things all I had to do is this:

virtual void setInitiator(QPainter* painter, QImage* image) = 0;

I'm guessing the assignment to zero forces a vtable to be created that holds a null pointer to the member function. This simple assignment fixed everything for me.

Keith
  • 49
  • 5