Results 1 to 8 of 8

Thread: undefined reference to vtable in class

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  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

    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.

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

    JeanC (26th February 2011)

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
  •  
Qt is a trademark of The Qt Company.