Results 1 to 8 of 8

Thread: QOBJECT and undefined reference to vtable errors

  1. #1
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QOBJECT and undefined reference to vtable errors

    I've been working on a plugin to an third party application an have recently introduced a GUI dialogue module into the project. The problem is that I can't get around an error I am getting which is:

    undefined reference to 'vtable for StelModule'

    I have reduced my code down to the basics and have discovered that when I remove Q_OBJECT from the class definition within the hpp file the everything compiles Ok. Obviously because this is a GUI it will require Q_OBJECT.

    I've found a few posts on the subject via Google and it would appear that this is something to do with .moc files. Unfortunately things start to get rather confusing after that, but as far as I understand, QT 4.7.1 (2010.05) should manage this pretty much authormatically. I have seen references to including a .moc file in the .cpp file but there seem to be no .moc files being generated. Insetad I do see a moc_mymodule.cpp and moc_mymodule.o in the debug directory. I'm also confused about what, if anything, should be added to the makefile and the qmake.conf file?

    Here is my code:


    HPP file:

    Qt Code:
    1. #ifndef GPSLOCATOR_HPP
    2. #define GPSLOCATOR_HPP
    3.  
    4. #include "StelGui.hpp"
    5. #include "StelModule.hpp"
    6. #include "StelFader.hpp"
    7. #include "StelPluginInterface.hpp"
    8.  
    9. class QTimer;
    10. class QPixmap;
    11. class StelButton;
    12. class GpsLocatorDialog;
    13.  
    14.  
    15. //! Dynamically loaded plug-in
    16.  
    17. class GpsLocator : public StelModule
    18. {
    19. Q_OBJECT
    20. public:
    21. GpsLocator();
    22. virtual ~GpsLocator();
    23.  
    24. };
    25.  
    26. /* COMMENTED OUT FOR TESTING
    27.   virtual ~GpsLocator();
    28.   // Methods defined in StelModule class
    29.   virtual void init();
    30.   virtual void update(double deltaTime);
    31.   virtual void draw(StelCore* core);
    32.   virtual double getCallOrder(StelModuleActionName actionName) const;
    33. signals:
    34.  
    35. public slots:
    36. // void enableGpsLocator(bool b);
    37.  
    38. private:
    39.   //Toolbar button
    40.   QPixmap* pixmapHover;
    41.   QPixmap* pixmapOnIcon;
    42.   QPixmap* pixmapOffIcon;
    43.   StelButton* toolbarButton;
    44.  
    45.   // Dialog Window
    46.   GpsLocatorDialog* dialogWindow;
    47.  
    48. };
    49. */
    50.  
    51. //! This class is used by Qt to manage a plug-in interface
    52. class GpsLocatorStelPluginInterface : public QObject, public StelPluginInterface
    53. {
    54. Q_OBJECT
    55. Q_INTERFACES(StelPluginInterface)
    56. public:
    57. virtual StelModule* getStelModule() const;
    58. virtual StelPluginInfo getPluginInfo() const;
    59. };
    60.  
    61. #endif // GPSLOCATOR_HPP
    To copy to clipboard, switch view to plain text mode 


    CPP file:

    Qt Code:
    1. #include "gpslocator.hpp"
    2.  
    3. #include "StelApp.hpp"
    4. #include "StelCore.hpp"
    5. #include "StelFileMgr.hpp"
    6. #include "StelGui.hpp"
    7. #include "StelGuiItems.hpp"
    8. #include "StelLocaleMgr.hpp"
    9. #include "StelModuleMgr.hpp"
    10. #include "StelPainter.hpp"
    11.  
    12. #include <QAction>
    13. #include <QDebug>
    14. #include <QPixmap>
    15. #include <QString>
    16.  
    17.  
    18. //! This method is the one called automatically by the StelModuleMgr
    19. //! just after loading the dynamic library
    20. StelModule* GpsLocatorStelPluginInterface::getStelModule() const
    21. {
    22. // return new GpsLocator();
    23. return false;
    24. }
    25.  
    26. StelPluginInfo GpsLocatorStelPluginInterface::getPluginInfo() const
    27. {
    28. // Allow to load the resources when used as a static plugin
    29. // Q_INIT_RESOURCE(gpsLocatorRsc);
    30.  
    31. StelPluginInfo info;
    32. info.id = "GpsLocator";
    33. info.displayedName = "GPS Locator Plugin";
    34. info.authors = "Jan Chajecki";
    35. info.contact = "http://stellarium.org/";
    36. info.description = "Allows Stellarium co-ordinates to be updated from a GPS device";
    37. return info;
    38. }
    39.  
    40. Q_EXPORT_PLUGIN2(GpsLocator, GpsLocatorStelPluginInterface)
    41.  
    42.  
    43. //! Constructor
    44. GpsLocator::GpsLocator()
    45. {
    46. // setObjectName("GpsLocator");
    47. }
    48.  
    49. //! Destructor
    50. GpsLocator::~GpsLocator()
    51. {
    52. }
    To copy to clipboard, switch view to plain text mode 
    Can anyone clearify what I need to do to get rid of this error please?
    Last edited by stargazer; 11th January 2011 at 17:11.

  2. #2
    Join Date
    Nov 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QOBJECT and undefined reference to vtable errors

    Usually when this happens you need to re-run qmake and make sure you've implemented all the slots for your class.

  3. #3
    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: QOBJECT and undefined reference to vtable errors

    undefined reference to 'vtable for StelModule'
    Where is StelModule defined, and where implemented?
    Can we see the code for that?
    Does StelModule derive from QObject - does is call Q_OBJECT macro?
    ==========================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.

  4. #4
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QOBJECT and undefined reference to vtable errors

    Is it OK to post someone else's code?

    high_flyer, the StelModule class is defined in StelModule.hpp. This is not one of my classes but part of an open source project that is licencsed under GNU. Since its accessible to all anyway so I'm sure there should be not problem in at least providing this snippet of the class definition:

    Qt Code:
    1. class StelModule : public QObject
    2. {
    3. // Do not add Q_OBJECT here!!
    4. // This make this class compiled by the Qt moc compiler and for some unknown reasons makes it impossible to dynamically
    5. // load plugins on windows.
    6. public:
    7. StelModule() {;}
    8.  
    9. virtual ~StelModule() {;}
    10.  
    11. //! Initialize itself.
    12. //! If the initialization takes significant time, the progress should be displayed on the loading bar.
    13. virtual void init() = 0;
    14.  
    15. //! Called before the module will be delete, and before the openGL context is suppressed.
    16. //! Deinitialize all openGL texture in this method.
    17. virtual void deinit() {;}
    18. etc...
    To copy to clipboard, switch view to plain text mode 

    It appears to be derived from the QObject class but it does not have the Q_OBJECT macro. I would point out though that the code for other plugins to this project, including the provided examples, have a Q_OBJECT macro in their class definition.

    Also, the presence of the Q_OBJECT macro in the GpsLocatorStelPluginInterface class does not seem to generate an error. I tested this by commenting out the GpsLocator class and its corresponding implementation in the CPP file. The remaining code compiled without errors.

    The comment after the class header is interesting and may be relevant, but I'm not sure what it means, except that evidently someone was having trouble with moc...
    Last edited by stargazer; 12th January 2011 at 10:41.

  5. #5
    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: QOBJECT and undefined reference to vtable errors

    Code snippets, specially of a class decleration will not be of much interest to anyone.
    At any rate, you can answer my last question, and check it in your code, with out posting any code.
    ==========================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.

  6. #6
    Join Date
    Jan 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QOBJECT and undefined reference to vtable errors

    Ok, noted for future reference!

    vpicaver, [I]'ve tried adding slots and signals but that made no difference.

    According to the information in the QT reference documentation, the QT_OBJECT macro is mandatory, but slots and signals are not.

    Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.
    However this seems to indicate that I need to run the Meta Object Compiler, however the information I found in the linked page suggests seems to state that this is already done automatically:

    We recommend using the qmake makefile generation tool for building your makefiles. This tool generates a makefile that does all the necessary moc handling.
    Isn't qmake run every time you compile?

  7. #7
    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: QOBJECT and undefined reference to vtable errors

    It appears to be derived from the QObject class but it does not have the Q_OBJECT macro. I would point out though that the code for other plugins to this project, including the provided examples, have a Q_OBJECT macro in their class definition.
    Lets make some things clear:
    the Q_OBJECT macro is only needed (and moc'ing), if you are using signals and slots.
    If you are not using signal/slots, you don't have to use Q_OBJECT and you don't have to moc that class.
    Therefore I would not put Q_OBJECT macro in the interface since this will force all implementing class to use it- which is not always what you want.
    ==========================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.

  8. #8
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QOBJECT and undefined reference to vtable errors

    I also got this problem and don't know how to solve it.
    I declare a prototype nested class in header file, declaration and definition in source file.

    Qt Code:
    1. class Service::Private : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. Private(const QString &aCMName, const QString &aName) {}
    6.  
    7. QString mName;
    8. QString mCMName;
    9. Tp::ConnectionManagerPtr mCM;
    10. QList<Service::ServiceParameter> mParameters;
    11.  
    12. public slots:
    13. void onCMReady(Tp::PendingOperation *operation) {}
    14. };
    To copy to clipboard, switch view to plain text mode 


    Added after 20 minutes:


    I see this problem on Meta-object compiler document, at Limitations:

    Nested Classes Cannot Have Signals or Slots
    Here's an example of the offending construct:
    Qt Code:
    1. class A
    2. {
    3. public:
    4. class B
    5. {
    6. Q_OBJECT
    7.  
    8. public slots: // WRONG
    9. void b();
    10. };
    11. };
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by OnlyK View Post
    I also got this problem and don't know how to solve it.
    I declare a prototype nested class in header file, declaration and definition in source file.

    Qt Code:
    1. class Service::Private : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. Private(const QString &aCMName, const QString &aName) {}
    6.  
    7. QString mName;
    8. QString mCMName;
    9. Tp::ConnectionManagerPtr mCM;
    10. QList<Service::ServiceParameter> mParameters;
    11.  
    12. public slots:
    13. void onCMReady(Tp::PendingOperation *operation) {}
    14. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by OnlyK; 25th April 2011 at 05:29.

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 in Threads.
    By prasanth.nvs in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 20th February 2009, 10:19
  3. Undefined reference to 'vtable for XXX'
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 15:59
  4. undefined reference to vtable
    By renjithmamman in forum Qt Programming
    Replies: 5
    Last Post: 2nd July 2006, 04:23
  5. 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.