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.

7
  • After a qmake you still have the error ? Commented May 3, 2020 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 Commented May 3, 2020 at 22:38
  • I think, that bullet while not MyRect, becasue, i will dd picture (different to bullet and MyRect which shots) Commented May 3, 2020 at 22:55
  • Don't know why you put the Q_MOC_RUN macro, remove it and test, works for me Commented May 3, 2020 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. Commented May 4, 2020 at 1:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.