Results 1 to 3 of 3

Thread: linker error when the class is added in main.cpp

  1. #1
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Platforms
    Unix/X11

    Default linker error when the class is added in main.cpp

    I took an example program from Qt for qquickview. I am getting an error(undefined reference to 'vtable for ApplicationData', collect2 ld returned 1 exit status) when I add the class in main.cpp. Here is the code

    Qt Code:
    1. #include<QQuickView>
    2. #include<QGuiApplication>
    3. #include<QObject>
    4. #include<QQmlContext>
    5. #include<QDateTime>
    6. //#include"applicationdata.h"
    7.  
    8. class ApplicationData : public QObject
    9. {
    10. Q_OBJECT
    11. public:
    12. Q_INVOKABLE QDateTime getCurrentDateTime() const {
    13. return QDateTime::currentDateTime();
    14. }
    15. };
    16.  
    17.  
    18. int main(int argc, char *argv[]) {
    19. QGuiApplication app(argc, argv);
    20.  
    21. QQuickView view;
    22.  
    23. ApplicationData data;
    24. view.rootContext()->setContextProperty("applicationData", &data);
    25.  
    26. view.setSource(QUrl::fromLocalFile("MyItem.qml"));
    27. view.show();
    28.  
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    I took the class and placed in applicationdata.h. I didn't get error and the output was perfect. what is the reason I can't understand.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: linker error when the class is added in main.cpp

    A cpp file is not automatically processed for Q_OBJECT, headers are.

    You can fix that by doing something like
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    at the end of main.cpp and re-running qmake

    Cheers,
    _

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

    bts-007 (2nd June 2013)

  4. #3
    Join Date
    May 2013
    Posts
    11
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Platforms
    Unix/X11

    Default Re: linker error when the class is added in main.cpp

    anda_skoa that solved the problem. I added this line in pro file

    Qt Code:
    1. INCLUDEPATH += tmp/moc/release_shared
    To copy to clipboard, switch view to plain text mode 

    what is the nedd for it

Similar Threads

  1. Replies: 2
    Last Post: 28th September 2012, 17:15
  2. Replies: 1
    Last Post: 20th January 2012, 04:39
  3. Replies: 8
    Last Post: 15th October 2011, 18:23
  4. Linker error with .obj
    By Wasabi in forum Newbie
    Replies: 0
    Last Post: 3rd August 2011, 16:49
  5. Linker error
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2008, 14:30

Tags for this Thread

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.