Results 1 to 4 of 4

Thread: undefined reference to vtable in Threads.

  1. #1
    Join Date
    Jul 2008
    Location
    Bangalore
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Unix/X11

    Default undefined reference to vtable in Threads.

    In the following code i got compilation erros like below

    /thread.cpp:20: undefined reference to `vtable for MyThread'

    thread.cpp:6: undefined reference to `vtable for MyThread'

    MyThread.h file is

    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QObject>
    6.  
    7. class MyThread:public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. MyThread(QWidget* aParent);
    12. ~MyThread();
    13.  
    14. public slots:
    15. void Finished();
    16.  
    17. protected:
    18. void run();
    19. };
    20.  
    21. #endif //MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    and MyThread.cpp is
    Qt Code:
    1. #include "thread.h"
    2. #include <iostream.h>
    3. #include <QMetaType>
    4.  
    5.  
    6. MyThread::MyThread(QWidget* aParent)
    7. {
    8. qRegisterMetaType<MyThread*>("MyThread");
    9. QObject::connect(this,SIGNAL(finished()),this,SLOT(Finished()));
    10. if(!isRunning())
    11. {
    12. start();
    13. }
    14. else
    15. {
    16. return;
    17. }
    18. }
    19.  
    20. MyThread::~MyThread()
    21. {
    22.  
    23. }
    24.  
    25. void MyThread::run()
    26. {
    27. cout <<"Hello :" << endl;
    28. }
    29.  
    30.  
    31. void MyThread::Finished()
    32. {
    33. cout << "In Finishes:" << endl;
    34. }
    To copy to clipboard, switch view to plain text mode 
    and main.cpp is

    Qt Code:
    1. int main(int argc,char* argv[])
    2. {
    3. QtopiaApplication app(argc,argv);
    4.  
    5. QWidget* lWidget = new QWidget;
    6. lWidget->setGeometry(0,0,240,320);
    7.  
    8. MyThread* lThread = new MyThread(lWidget);
    9.  
    10. Widget->show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Now whts the problem in above code and wts the reason for that.
    Last edited by jpn; 6th February 2009 at 13:34. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: undefined reference to vtable in Threads.

    Is thread.h listed in .pro? Did you re-run qmake after adding Q_OBJECT?
    J-P Nurmi

  3. #3
    Join Date
    Dec 2007
    Posts
    40
    Thanks
    6
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: undefined reference to vtable in Threads.

    Quote Originally Posted by prasanth.nvs View Post
    In the following code i got compilation erros like below

    /thread.cpp:20: undefined reference to `vtable for MyThread'

    thread.cpp:6: undefined reference to `vtable for MyThread'

    MyThread.h file is

    Qt Code:
    1. #ifndef MYTHREAD_H
    2. #define MYTHREAD_H
    3.  
    4. #include <QThread>
    5. #include <QObject>
    6.  
    7. class MyThread:public QThread
    8. {
    9. Q_OBJECT
    10. public:
    11. MyThread(QWidget* aParent);
    12. ~MyThread();
    13.  
    14. public slots:
    15. void Finished();
    16.  
    17. protected:
    18. void run();
    19. };
    20.  
    21. #endif //MYTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    and MyThread.cpp is
    Qt Code:
    1. #include "thread.h"
    2. #include <iostream.h>
    3. #include <QMetaType>
    4.  
    5.  
    6. MyThread::MyThread(QWidget* aParent)
    7. {
    8. qRegisterMetaType<MyThread*>("MyThread");
    9. QObject::connect(this,SIGNAL(finished()),this,SLOT(Finished()));
    10. if(!isRunning())
    11. {
    12. start();
    13. }
    14. else
    15. {
    16. return;
    17. }
    18. }
    19.  
    20. MyThread::~MyThread()
    21. {
    22.  
    23. }
    24.  
    25. void MyThread::run()
    26. {
    27. cout <<"Hello :" << endl;
    28. }
    29.  
    30.  
    31. void MyThread::Finished()
    32. {
    33. cout << "In Finishes:" << endl;
    34. }
    To copy to clipboard, switch view to plain text mode 
    and main.cpp is

    Qt Code:
    1. int main(int argc,char* argv[])
    2. {
    3. QtopiaApplication app(argc,argv);
    4.  
    5. QWidget* lWidget = new QWidget;
    6. lWidget->setGeometry(0,0,240,320);
    7.  
    8. MyThread* lThread = new MyThread(lWidget);
    9.  
    10. Widget->show();
    11.  
    12. return app.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Now whts the problem in above code and wts the reason for that.
    you are trying to call start() which in turn calls the void run() method from inside your MyThread constructor. this is not valid. before the object construction is complete, you cannot start calling the virtual methods of the object. this is a classic C++ error!

    cheers!
    Let your work talk for you

  4. The following user says thank you to jogeshwarakundi for this useful post:

    prasanth.nvs (19th February 2009)

  5. #4
    Join Date
    Feb 2009
    Posts
    7
    Qt products
    Platforms
    Unix/X11

    Default Re: undefined reference to vtable in Threads.

    This problem occurs due to mistakes of Profile like Spellings of Variables.

Similar Threads

  1. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  2. MS Sql native driver??
    By LordQt in forum Qt Programming
    Replies: 4
    Last Post: 9th October 2007, 13:41
  3. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 14:28
  4. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 19:15
  5. Strange error while using Q3Canvas
    By Kapil in forum Newbie
    Replies: 13
    Last Post: 15th June 2006, 19:36

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.