May 19, 2011

franziss franziss
Lab Rat
50 posts

undefined reference to ‘vtable for myClass’

 

I have this undefined reference to ‘vtable for myClass’ problem. Why is it so?

I have a header file myClass.h which contains a slot function

  1. #ifndef MYCLASS_H
  2. #define MYCLASS_H
  3. #include <QObject>
  4. #include "qdebug.h"
  5.  
  6. class myClass : public QObject
  7. {
  8.     Q_OBJECT
  9.  
  10. public slots:
  11.     void cppSlot(int number);
  12.  
  13. };
  14.  
  15. #endif // MYCLASS_H

A cpp file myClass.cpp

  1. #include "myclass.h"
  2.  
  3. void myClass::cppSlot(int number) {
  4.     qDebug() << "Called the C++ slot with" << number;
  5. }

with main file main.cpp

  1. #include <QtGui/QApplication>
  2. #include "qmlapplicationviewer.h"
  3. #include "QDeclarativeContext"
  4. #include "myclass.h"
  5.  
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     QApplication app(argc, argv);
  10.  
  11.     QmlApplicationViewer viewer;
  12.     myClass MyClass;
  13.     viewer.rootContext()->setContextProperty("myObject", &MyClass);
  14.     viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
  15.     viewer.setMainQmlFile(QLatin1String("qml/test1/main.qml"));
  16.     viewer.showExpanded();
  17.  
  18.     return app.exec();
  19. }

I have also spcific the header and cpp file in my .pro file.

  1. # Add more folders to ship with the application, here
  2. folder_01.source = qml/test1
  3. folder_01.target = qml
  4. DEPLOYMENTFOLDERS = folder_01
  5.  
  6. # Additional import path used to resolve QML modules in Creator's code model
  7. QML_IMPORT_PATH =
  8.  
  9. symbian:TARGET.UID3 = 0xE6EDC966
  10.  
  11. # Allow network access on Symbian
  12. symbian:TARGET.CAPABILITY += NetworkServices
  13.  
  14. # Define QMLJSDEBUGGER to allow debugging of QML in debug builds
  15. # (This might significantly increase build time)
  16. # DEFINES += QMLJSDEBUGGER
  17.  
  18. # If your application uses the Qt Mobility libraries, uncomment
  19. # the following lines and add the respective components to the
  20. # MOBILITY variable.
  21. # CONFIG += mobility
  22. # MOBILITY +=
  23.  
  24. # The .cpp file which was generated for your project. Feel free to hack it.
  25. SOURCES += main.cpp \
  26.     myclass.cpp
  27.  
  28. # Please do not modify the following two lines. Required for deployment.
  29. include(qmlapplicationviewer/qmlapplicationviewer.pri)
  30. qtcAddDeployment()
  31.  
  32. HEADERS += \
  33.     myclass.h

5 replies

May 19, 2011

koahnig koahnig
Mad Scientist
2099 posts

Hi,
it would be good to see the complete error message as well.

May 19, 2011

franziss franziss
Lab Rat
50 posts

Hi koahnig,

The error message is as follows:

  1. debug/main.o: In function `myClass':
  2. C:\Laptop\Dropbox\Qt\examples\test1-build-desktop/../test1//myclass.h:7: undefined reference to `vtable for myClass'
  3. debug/main.o: In function `~myClass':
  4. C:\Laptop\Dropbox\Qt\examples\test1-build-desktop/../test1//myclass.h:7: undefined reference to `vtable for myClass'
  5. collect2: ld returned 1 exit status
  6. mingw32-make[1]: *** [debug\test1.exe] Error 1
  7. mingw32-make: *** [debug] Error 2
  8. The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
  9. Error while building project test1 (target: Desktop)
  10. When executing build step 'Make'

May 19, 2011

yshurik yshurik
Lab Rat
40 posts

you may need to qmake+rebuild, also you may try to define at least virtual destructor

May 19, 2011

peppe peppe
Ant Farmer
1025 posts

http://doc.qt.nokia.com/4.7/debug.html#common-bugs

(moc wasn’t run on your header, and/or the resulting cpp wasn’t linked in. rerun qmake)

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

May 19, 2011

franziss franziss
Lab Rat
50 posts

Hi Guys,

Thanks for your help, I have moc and I have added

  1. HEADERS += \
  2.     myclass.h
  3. SOURCES += main.cpp \
  4.     myclass.cpp

in my .pro file. I found my mistake, I must also add

  1. CONFIG += qt

It works now, even though I don’t know why. =)

 
  ‹‹ Any idea when use OAuth with QML?      Loader child element ››

You must log in to post a reply. Not a member yet? Register here!