Results 1 to 11 of 11

Thread: Undifined Refrence: vtable

  1. #1

    Default Undifined Refrence: vtable

    I just started learning Qt and I have been working through the example on this page: http://doc.qt.nokia.com/4.7/gettingstartedqt.html
    The problem is that when I get to sub classing QWidget I start getting the following linker errors:

    notepad.cpp|3|undefined reference to `vtable for Notepad'|
    obj\Debug\notepad.o:C:\programming\c\test\notepad. h|10|undefined reference to `Notepad::staticMetaObject'|

    The code is exactly the same as the examples, I am using Qt 4.7.1 and codeblocks as the ide.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Undifined Refrence: vtable

    Clean you project (that is, remove any generated files), run qmake again, rebuild

  3. #3
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Undifined Refrence: vtable

    Quote Originally Posted by mweber1488 View Post
    I just started learning Qt and I have been working through the example on this page: http://doc.qt.nokia.com/4.7/gettingstartedqt.html
    The problem is that when I get to sub classing QWidget I start getting the following linker errors:

    notepad.cpp|3|undefined reference to `vtable for Notepad'|
    obj\Debug\notepad.o:C:\programming\c\test\notepad. h|10|undefined reference to `Notepad::staticMetaObject'|

    The code is exactly the same as the examples, I am using Qt 4.7.1 and codeblocks as the ide.
    The moc output for notepad isn't getting linked in.
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Undifined Refrence: vtable

    Quote Originally Posted by nroberts View Post
    The moc output for notepad isn't getting linked in.
    and the solution to fix that is in post #2

  5. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Undifined Refrence: vtable

    When you sub-class don't forget the Q_OBJECT macro:
    Qt Code:
    1. class Notepad : public QWidget {
    2. Q_OBJECT //this one is important if you use signals/slots or other qmetaobject goodies
    3. //.... rest of the class definition
    To copy to clipboard, switch view to plain text mode 

    Also for each class use a header .h file for declaration and a .cpp file for definitions.

    If you write classes with Q_OBJECT in the main.cpp file (a and have only that one .cpp file) you should use:
    Qt Code:
    1. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    at the end of the .cpp file
    Replace main.cpp with YourFileName.cpp

  6. #6

    Default Re: Undifined Refrence: vtable

    Quote Originally Posted by squidge View Post
    and the solution to fix that is in post #2
    Actually its not as
    codeblocks as the ide
    and doesn't use qmake.
    So I figure out the moc thing just before I looked here as I tried building under QtCreator but now I am getting the following error (in both QtCreator and codeblocks):
    Qt Code:
    1. debug/moc_notepad.o:C:\programming\c\test\test-build-desktop/debug/moc_notepad.cpp:74: undefined reference to `Notepad::open()'
    2.  
    3. debug/moc_notepad.o:C:\programming\c\test\test-build-desktop/debug/moc_notepad.cpp:75: undefined reference to `Notepad::save()'
    4.  
    5. debug/moc_notepad.o:C:\programming\c\test\test-build-desktop/debug/moc_notepad.cpp:76: undefined reference to `Notepad::quit()'
    To copy to clipboard, switch view to plain text mode 

    For completeness here is the entire code:
    notepad.h
    Qt Code:
    1. #ifndef _NOTEPAD_H_
    2. #define _NOTEPAD_H_
    3.  
    4. #include <QtGui>
    5.  
    6. class Notepad: public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. Notepad();
    12.  
    13. private slots:
    14. void open();
    15. void save();
    16. void quit();
    17.  
    18. private:
    19. QTextEdit *textEdit; // The text editor
    20.  
    21. QAction *openAction; // For the widgets that perform open
    22. QAction *saveAction; // Save shit
    23. QAction *quitAction; // Quit shit
    24.  
    25. QMenu *fileMenu; // Holds the menu options
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 
    notepad.cpp
    Qt Code:
    1. #include "notepad.h"
    2.  
    3. Notepad::Notepad()
    4. {
    5. // Do the actions first
    6. openAction = new QAction(tr("&Open"), this);
    7. saveAction = new QAction(tr("&Sace"), this);
    8. quitAction = new QAction(tr("E&xit"), this);
    9.  
    10. // Now connect the signals to the actions
    11. connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
    12. connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
    13. connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
    14.  
    15. // Now set the menu bar
    16. fileMenu = menuBar()->addMenu(tr("&File"));
    17. fileMenu->addAction(openAction);
    18. fileMenu->addAction(saveAction);
    19. fileMenu->addSeparator();
    20. fileMenu->addAction(quitAction);
    21.  
    22. textEdit = new QTextEdit;
    23. setCentralWidget(textEdit);
    24.  
    25. setWindowTitle(tr("Notepad"));
    26. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include "notepad.h"
    2.  
    3. int main(int argc, char ** argv)
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. Notepad notepad;
    8. notepad.show();
    9.  
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Undifined Refrence: vtable

    The error messages you are getting now tell you what is wrong:
    undefined reference to `Notepad:: open()'
    You've declared your slots but you haven't defined them.
    Last edited by norobro; 5th January 2011 at 02:36. Reason: Got rid of a smilie

  8. #8

    Default Re: Undifined Refrence: vtable

    Ahh, the example I have been following failed to mention that and I have been using D for so long that I am not used to classes being in stops. Thanks for all the help folks.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Undifined Refrence: vtable

    As always:
    1. Learn how your tools work
    2. Provide all your information if you want help. My crystal ball often doesn't see if you use qmake or not.

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Undifined Refrence: vtable

    It is always recommended that if your just getting started with Qt, you should make life at least a little easier for yourself by using the Qt SDK (ie, Qt Creator) and THEN moving to your preferred IDE when you understand how everything works rather than trying to jump in with a new framework from the start.

  11. #11
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Undifined Refrence: vtable

    Quote Originally Posted by mweber1488 View Post
    I am not used to classes being in stops.
    What be this?
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

Similar Threads

  1. error: undefined refrence
    By motsh in forum Qt for Embedded and Mobile
    Replies: 5
    Last Post: 3rd May 2009, 15:22
  2. Undefined reference to 'vtable for XXX'
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 15:59
  3. vtable problems
    By high_flyer in forum Qt Programming
    Replies: 6
    Last Post: 3rd September 2007, 13:52
  4. silly undefined refrence to main
    By quickNitin in forum Newbie
    Replies: 3
    Last Post: 16th November 2006, 09:41
  5. Replies: 1
    Last Post: 18th June 2006, 10:12

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.