Results 1 to 8 of 8

Thread: undefined reference to vtable in class

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default undefined reference to vtable in class

    Hello,

    I am trying to use this code and I am getting the error 'undefined reference to `vtable for ClientHandler', on the lines indicated with << on this line
    It's supposed to become a simple console program to update my dns subscriptions on namecheap and dyndns.

    Qt Code:
    1. #include <QDebug>
    2. #include <QNetworkAccessManager>
    3. #include <QUrl>
    4. #include <QNetworkRequest>
    5. #include <QObject>
    6.  
    7. class ClientHandler : public QObject
    8. { // << on this line
    9. Q_OBJECT
    10. QNetworkAccessManager *manager;
    11. private slots:
    12. void replyFinished(QNetworkReply *);
    13. public:
    14. void CheckSite(void);
    15. ~ClientHandler();
    16. };
    17.  
    18. void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
    19. ClientHandler::~ClientHandler() {} // <<-- on this line
    20.  
    21. void ClientHandler::CheckSite(void)
    22. {
    23. QUrl qrl("http://checkip.dyndns.org");
    24. manager = new QNetworkAccessManager(this);
    25. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    26. manager->get(QNetworkRequest(qrl));
    27. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for help!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to vtable in class

    This header is probably not getting moc'ed, or the moc result location is not known to the compiler.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: undefined reference to vtable in class

    No problems with compiling this code here.
    Added this to header:
    Qt Code:
    1. #include <QDebug>
    2. #include <QNetworkAccessManager>
    3. #include <QUrl>
    4. #include <QNetworkRequest>
    5. #include <QObject>
    6.  
    7. class ClientHandler : public QObject
    8. { // << on this line
    9. Q_OBJECT
    10. QNetworkAccessManager *manager;
    11. private slots:
    12. void replyFinished(QNetworkReply *);
    13. public:
    14. void CheckSite(void);
    15. ~ClientHandler();
    16. };
    To copy to clipboard, switch view to plain text mode 
    This to .cpp:
    Qt Code:
    1. void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
    2. ClientHandler::~ClientHandler() {} // <<-- on this line
    3.  
    4. void ClientHandler::CheckSite(void)
    5. {
    6. QUrl qrl("http://checkip.dyndns.org");
    7. manager = new QNetworkAccessManager(this);
    8. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    9. manager->get(QNetworkRequest(qrl));
    10. }
    To copy to clipboard, switch view to plain text mode 
    Included header in main.cpp and I can use ClientHandler objects:
    Qt Code:
    1. #include <QApplication>
    2. #include "ClientHandler.h"
    3. int main( int argc, char ** argv ){
    4. QApplication app(argc,argv);
    5. ClientHandler h;
    6. return 0;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Then simply: qmake, make - compiled without errors.
    Maybe try to do clean rebuild ( make clean, qmake, make ).

  4. #4
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to vtable in class

    Thanks to both of you.
    Unfortunately I don't understand both of you.
    @high_flyer I am rather new to Qt (played with it some years ago) so I don't understand what you mean. I am using Qt Creator so I am not used to moc.
    @stampede for the life of me, I don't see what changes you made to the code. I already have a main() in there:

    Qt Code:
    1. #include <QSettings>
    2. #include <QDebug>
    3. #include <QNetworkAccessManager>
    4. #include <QUrl>
    5. #include <QNetworkRequest>
    6. #include <QObject>
    7.  
    8. class ClientHandler : public QObject
    9. {
    10. Q_OBJECT
    11. QNetworkAccessManager *manager;
    12. private slots:
    13. void replyFinished(QNetworkReply *);
    14. public:
    15. void CheckSite(void);
    16. ~ClientHandler();
    17. };
    18.  
    19. void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
    20. ClientHandler::~ClientHandler() {}
    21.  
    22. void ClientHandler::CheckSite(void)
    23. {
    24. QUrl qrl("http://checkip.dyndns.org");
    25. manager = new QNetworkAccessManager(this);
    26. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    27. manager->get(QNetworkRequest(qrl));
    28. }
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QSettings ini("Jean", "ddns");
    33. if (!ini.contains("LastIp"))
    34. {
    35. qDebug() << "no ip";
    36. ClientHandler c;
    37. c.CheckSite();
    38. }
    39. return 0;
    40. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: undefined reference to vtable in class

    I have separated the code into three files: header and .cpp for the class, and third file for main() function.
    As high_flyer said, the class meta code is not generated, so you are getting undefined references.
    I haven't changed anything in the code itself, just when you add the class header to the list of headers in your .pro file, it gets "moc-ed", the Meta-Object code is created in moc_headerName.cpp. You don't have the header file, so this code is not generated.
    Split this into three files and add header to project .pro file.
    I think you can use the "moc" on your file manually, but splitting it makes more sense to me.
    ----
    Edit:
    Split this into three files and add header to project .pro file.
    Add all of them to .pro file, .cpp to SOURCES and header to HEADERS
    Last edited by stampede; 25th February 2011 at 13:39.

  6. #6
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to vtable in class

    Thank you, I understand.
    Personally I think it's a bit overkill to use separate files for such a small program, against my taste, but if that's what Qt wants, ok.
    Now I tried to add that class in Qt Creator but I can't find how. Off course I could manually edit the .pro file but it seems a bit odd that I cannot do that in Creator. If I try to edit the .pro file in Creator it just ignores my 'File Open'.

    I changed things by hand, now I get:
    QObject::startTimer: QTimer can only be used with threads started with QThread
    and
    QObject::connect: Cannot connect (null)::destroyed() to QHostInfoLookupManager::waitForThreadPoolDone()

    My guess is that the class is destroyed before it can do anything. How do I avoid that? I'd like to keep this a small console program.
    Last edited by JeanC; 25th February 2011 at 14:27.

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: undefined reference to vtable in class

    If I try to edit the .pro file in Creator it just ignores my 'File Open'.
    Really ? It opens the .pro for editing when I double click it.
    If you really want, you can keep it as one file:
    Qt Code:
    1. #include <QSettings>
    2. #include <QDebug>
    3. #include <QNetworkAccessManager>
    4. #include <QUrl>
    5. #include <QNetworkRequest>
    6. #include <QObject>
    7.  
    8. class ClientHandler : public QObject
    9. {
    10. Q_OBJECT
    11. QNetworkAccessManager *manager;
    12. private slots:
    13. void replyFinished(QNetworkReply *);
    14. public:
    15. void CheckSite(void);
    16. ~ClientHandler();
    17. };
    18.  
    19. void ClientHandler::replyFinished(QNetworkReply *reply) { qDebug() << "DONE"; }
    20. ClientHandler::~ClientHandler() {}
    21.  
    22. void ClientHandler::CheckSite(void)
    23. {
    24. QUrl qrl("http://checkip.dyndns.org");
    25. manager = new QNetworkAccessManager(this);
    26. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    27. manager->get(QNetworkRequest(qrl));
    28. }
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QSettings ini("Jean", "ddns");
    33. if (!ini.contains("LastIp"))
    34. {
    35. qDebug() << "no ip";
    36. ClientHandler c;
    37. c.CheckSite();
    38. }
    39. return 0;
    40. }
    41.  
    42. #include "moc_main.cpp"
    To copy to clipboard, switch view to plain text mode 
    in order to get "moc_main.cpp" file you need to run "moc":
    Qt Code:
    1. moc main.cpp -o moc_main.cpp
    To copy to clipboard, switch view to plain text mode 
    You don't need to add anything in the pro file this way.
    And yes, your ClientHandler goes out of scope and is deleted before finishing the network request:
    Qt Code:
    1. ...
    2. c.CheckSite();
    3. } // c goes out of scope here
    To copy to clipboard, switch view to plain text mode 
    How do I avoid that?
    One of possible solutions is to use QCoreApplication:
    Qt Code:
    1. int main(int argc,char**argv){
    2. QCoreApplication app(argc,argv);
    3. QSettings ini("Jean", "ddns");
    4. if (!ini.contains("LastIp"))
    5. {
    6. qDebug() << "no ip";
    7. ClientHandler c;
    8. c.CheckSite();
    9. }
    10. return app.exec();
    11. }
    12.  
    13. //
    14. void ClientHandler::replyFinished(QNetworkReply *reply) {
    15. qDebug() << "DONE";
    16. qApp->quit();
    17. }
    To copy to clipboard, switch view to plain text mode 
    I suppose it would be better to connect to QNetworkReply signals as well, as in example from QNetworkAccessManager docs ,in order to handle errors.
    -----
    sorry, now i see the wrong code ( c was deleted anyway ), should be something like:
    Qt Code:
    1. if (!ini.contains("LastIp"))
    2. {
    3. qDebug() << "no ip";
    4. ClientHandler c;
    5. c.CheckSite();
    6. app.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stampede; 25th February 2011 at 16:44.

  8. The following user says thank you to stampede for this useful post:

    JeanC (26th February 2011)

  9. #8
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: undefined reference to vtable in class

    You are a great help, thank you!

    Yes indeed nothing happens if I doubleclick ddns.pro, really strange..

    The project is back to one file again, turned out I had to install libqt4-dev in order to use moc.
    And I used QCoreApplication, I finally understand what that's for.
    Ater applying your suggestions, it now compiles, runs and even works.
    I added some errorchecking and the cleaning up for QNetworkReply, I think it's ok.
    Now I can go on! (making mistakes )
    I'm gonna try to make that class into a blocking / synchronous method like it used to be before qt 4.6 I think.

    Have a fine day!

    Qt Code:
    1. void ClientHandler::replyFinished(QNetworkReply *reply)
    2. {
    3. if (reply->error() != 0)
    4. qDebug() << "Error";
    5. else
    6. {
    7. QByteArray a = reply->readAll();
    8. qDebug() << a;
    9. qDebug() << "DONE";
    10. }
    11. reply->deleteLater();
    12. qApp->quit();
    13. }
    To copy to clipboard, switch view to plain text mode 

    You are a great help, thank you!

    Yes indeed nothing happens if I doubleclick ddns.pro, really strange..

    The project is back to one file again, turned out I had to install libqt4-dev in order to use moc.
    And I used QCoreApplication, I finally understand what that's for.
    Ater applying your suggestions, it now compiles, runs and even works.
    I added some errorchecking and the cleaning up for QNetworkReply, I think it's ok.
    Now I can go on! (making mistakes )
    I'm gonna try to make that class into a blocking / synchronous method like it used to be before qt 4.6 I think.

    Have a fine day!

    Qt Code:
    1. void ClientHandler::replyFinished(QNetworkReply *reply)
    2. {
    3. if (reply->error() != 0)
    4. qDebug() << "Error";
    5. else
    6. {
    7. QByteArray a = reply->readAll();
    8. qDebug() << a;
    9. qDebug() << "DONE";
    10. }
    11. reply->deleteLater();
    12. qApp->quit();
    13. }
    To copy to clipboard, switch view to plain text mode 


    Added after 39 minutes:


    I made it a blocking / synchronous class:

    Qt Code:
    1. #include <QProcess>
    2. #include <QNetworkAccessManager>
    3. #include <QUrl>
    4. #include <QNetworkRequest>
    5. #include <QNetworkReply>
    6. #include <QObject>
    7. #include <QCoreApplication>
    8.  
    9. class Http : public QObject
    10. {
    11. Q_OBJECT
    12. QNetworkAccessManager *manager;
    13. private slots:
    14. void replyFinished(QNetworkReply *);
    15. public:
    16. bool ready;
    17. QString Reply;
    18. ~Http() {};
    19. };
    20.  
    21. void Http::replyFinished(QNetworkReply *reply)
    22. {
    23. if (reply->error() != 0)
    24. {
    25. Reply = "Error";
    26. }
    27. else
    28. {
    29. Reply = reply->readAll();
    30. }
    31. ready = true;
    32. reply->deleteLater();
    33. }
    34.  
    35. QString Http::Get(QString S)
    36. {
    37. QUrl qrl(S);
    38. manager = new QNetworkAccessManager(this);
    39. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    40. manager->get(QNetworkRequest(qrl));
    41. ready = false;
    42. while (!ready)
    43. qApp->processEvents();
    44. return Reply;
    45. }
    To copy to clipboard, switch view to plain text mode 

    Useage:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication app(argc,argv);
    4. Http http;
    5. QString S = http.Get("http://www.someurl.com");
    6. etc.
    To copy to clipboard, switch view to plain text mode 

    Feel free to comment.
    Last edited by JeanC; 26th February 2011 at 10:57.

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 for XXX'
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 15:59
  3. undefined reference to vtable
    By renjithmamman in forum Qt Programming
    Replies: 5
    Last Post: 2nd July 2006, 04:23
  4. 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.