Results 1 to 6 of 6

Thread: undefined reference to 'vtable ...'

  1. #1
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default undefined reference to 'vtable ...'

    I don't understand this error:
    Qt Code:
    1. Undefined reference to 'vtable for Spaceship' Spaceship.cpp 9
    2. Undefined reference to 'vtable for Spaceship' Spaceship.cpp 9
    3. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    spaceship.h:
    Qt Code:
    1. #ifndef SPACESHIP_H
    2. #define SPACESHIP_H
    3.  
    4. #include <QGraphicsItem>
    5. #include <QObject>
    6.  
    7. class Spaceship : public QGraphicsItem
    8. {
    9.  
    10. public:
    11. Spaceship();
    12.  
    13. QRectF boundingRect() const;
    14. QPainterPath shape() const;
    15. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    16.  
    17. protected:
    18. void advance(int step);
    19.  
    20. private:
    21. qreal angle;
    22. qreal speed;
    23. QColor color;
    24. };
    25.  
    26. #endif // SPACESHIP_H
    To copy to clipboard, switch view to plain text mode 

    spaceship.cpp:
    Qt Code:
    1. #include "spaceship.h"
    2. #include <QGraphicsScene>
    3. #include <QPainter>
    4. #include <QStyleOption>
    5. #include <QObject>
    6. #include <math.h>
    7.  
    8. Spaceship::Spaceship()
    9. {
    10. setRotation(0);
    11. }
    12.  
    13. void Spaceship::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
    14. {
    15. //Draw the Spaceship
    16. painter->setBrush(Qt::black);
    17. painter->drawRect(10, 10, 20, 20);
    18. }
    19.  
    20. void Spaceship::advance(int step)
    21. {
    22. if (!step)
    23. return;
    24.  
    25. angle = 0;
    26. speed += 1;
    27. setPos(mapToParent(0, -(3 + speed) *3));
    28. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include "spaceship.h"
    2. #include <QtGui>
    3. #include <math.h>
    4. #include <QObject>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9. scene.setSceneRect(-300, -300, 600, 600);
    10. scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    11.  
    12. Spaceship *ship = new Spaceship;
    13. ship->setPos(0,0);
    14. scene.addItem(ship);
    15.  
    16. QGraphicsView view(&scene);
    17. view.setRenderHint(QPainter::Antialiasing);
    18.  
    19. view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Spaceship"));
    20. view.resize(400, 300);
    21. view.show();
    22.  
    23. QTimer timer;
    24. QObject::connect(&timer, SIGNAL(timeout()),&scene, SLOT(advance()));
    25. timer.start(1000 / 33);
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

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

    Thank you very much

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: undefined reference to 'vtable ...'

    Have you defined this two member functions: QRectF boundingRect() const;, QPainterPath shape() const;?

  3. The following user says thank you to Zlatomir for this useful post:

    GUIman (10th March 2011)

  4. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: undefined reference to 'vtable ...'

    Also, make sure you've added both the header and the implementation files to your .pro file.

  5. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: undefined reference to 'vtable ...'

    try cleaning the project, remove debug/release folders, remove the makefiles and the .pro.user file, then open the project again and rebuild.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: undefined reference to 'vtable ...'

    You need to provide the implementations for two virtual functions (one pure) you declare:
    Qt Code:
    1. QRectF boundingRect() const;
    2. QPainterPath shape() const;
    To copy to clipboard, switch view to plain text mode 
    and the error goes away.

  7. The following user says thank you to ChrisW67 for this useful post:

    GUIman (10th March 2011)

  8. #6
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: undefined reference to 'vtable ...'

    Thank you very much... I am an idiot

Similar Threads

  1. Qt Undefined Reference to vtable
    By ctote in forum Qt Programming
    Replies: 18
    Last Post: 24th February 2010, 22:24
  2. undefined reference to vtable in Threads.
    By prasanth.nvs in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th February 2009, 10:19
  3. Undefined reference to 'vtable for XXX'
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 15:59
  4. undefined reference to vtable
    By renjithmamman in forum Qt Programming
    Replies: 5
    Last Post: 2nd July 2006, 04:23
  5. Replies: 2
    Last Post: 30th June 2006, 18:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.