PDA

View Full Version : linker error when the class is added in main.cpp



bts-007
1st June 2013, 11:35
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



#include<QQuickView>
#include<QGuiApplication>
#include<QObject>
#include<QQmlContext>
#include<QDateTime>
//#include"applicationdata.h"

class ApplicationData : public QObject
{
Q_OBJECT
public:
Q_INVOKABLE QDateTime getCurrentDateTime() const {
return QDateTime::currentDateTime();
}
};


int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);

QQuickView view;

ApplicationData data;
view.rootContext()->setContextProperty("applicationData", &data);

view.setSource(QUrl::fromLocalFile("MyItem.qml"));
view.show();

return app.exec();
}


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.

anda_skoa
1st June 2013, 16:51
A cpp file is not automatically processed for Q_OBJECT, headers are.

You can fix that by doing something like


#include "main.moc"

at the end of main.cpp and re-running qmake

Cheers,
_

bts-007
2nd June 2013, 07:12
anda_skoa that solved the problem. I added this line in pro file



INCLUDEPATH += tmp/moc/release_shared


what is the nedd for it