Results 1 to 5 of 5

Thread: [SOLVED] Qt: Signals and slots Error: undefined reference to `vtable for

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Unhappy [SOLVED] Qt: Signals and slots Error: undefined reference to `vtable for

    Following example from this link: http://developer.kde.org/documentati...3lev1sec3.html

    Qt Code:
    1. #include <QObject>
    2. #include <QPushButton>
    3. #include <iostream>
    4. using namespace std;
    5.  
    6. class MyWindow : public QWidget
    7. {
    8. Q_OBJECT // Enable slots and signals
    9. public:
    10. MyWindow();
    11. private slots:
    12. void slotButton1();
    13. void slotButton2();
    14. void slotButtons();
    15. private:
    16. QPushButton *button1;
    17. QPushButton *button2;
    18. };
    19.  
    20. MyWindow :: MyWindow() : QWidget()
    21. {
    22. // Create button1 and connect button1->clicked() to this->slotButton1()
    23. button1 = new QPushButton("Button1", this);
    24. button1->setGeometry(10,10,100,40);
    25. button1->show();
    26. connect(button1, SIGNAL(clicked()), this, SLOT(slotButton1()));
    27.  
    28. // Create button2 and connect button2->clicked() to this->slotButton2()
    29. button2 = new QPushButton("Button2", this);
    30. button2->setGeometry(110,10,100,40);
    31. button2->show();
    32. connect(button2, SIGNAL(clicked()), this, SLOT(slotButton2()));
    33.  
    34. // When any button is clicked, call this->slotButtons()
    35. connect(button1, SIGNAL(clicked()), this, SLOT(slotButtons()));
    36. connect(button2, SIGNAL(clicked()), this, SLOT(slotButtons()));
    37. }
    38.  
    39. // This slot is called when button1 is clicked.
    40. void MyWindow::slotButton1()
    41. {
    42. cout << "Button1 was clicked" << endl;
    43. }
    44.  
    45. // This slot is called when button2 is clicked
    46. void MyWindow::slotButton2()
    47. {
    48. cout << "Button2 was clicked" << endl;
    49. }
    50.  
    51. // This slot is called when any of the buttons were clicked
    52. void MyWindow::slotButtons()
    53. {
    54. cout << "A button was clicked" << endl;
    55. }
    56.  
    57. int main ()
    58. {
    59. MyWindow a;
    60. }
    To copy to clipboard, switch view to plain text mode 
    results in:

    Qt Code:
    1. [13:33:45 Mon May 02] ~/junkPrograms/src/nonsense $ls
    2. nonsense.pro signalsSlots.cpp
    3.  
    4. [13:33:46 Mon May 02] ~/junkPrograms/src/nonsense $qmake
    5. [13:33:50 Mon May 02] ~/junkPrograms/src/nonsense $make
    6. g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/opt/qtsdk-2010.05/qt/mkspecs/linux-g++-64 -I. -I/opt/qtsdk-2010.05/qt/include/QtCore -I/opt/qtsdk-2010.05/qt/include/QtGui -I/opt/qtsdk-2010.05/qt/include -I. -I. -o signalsSlots.o signalsSlots.cpp
    7. g++ -m64 -Wl,-O1 -Wl,-rpath,/opt/qtsdk-2010.05/qt/lib -o nonsense signalsSlots.o -L/opt/qtsdk-2010.05/qt/lib -lQtGui -L/opt/qtsdk-2010.05/qt/lib -L/usr/X11R6/lib64 -lQtCore -lpthread
    8. signalsSlots.o: In function `MyWindow::MyWindow()':
    9. signalsSlots.cpp:(.text+0x1a2): undefined reference to `vtable for MyWindow'
    10. signalsSlots.cpp:(.text+0x1aa): undefined reference to `vtable for MyWindow'
    11. signalsSlots.o: In function `MyWindow::MyWindow()':
    12. signalsSlots.cpp:(.text+0x3e2): undefined reference to `vtable for MyWindow'
    13. signalsSlots.cpp:(.text+0x3ea): undefined reference to `vtable for MyWindow'
    14. signalsSlots.o: In function `main':
    15. signalsSlots.cpp:(.text+0x614): undefined reference to `vtable for MyWindow'
    16. signalsSlots.o:signalsSlots.cpp:(.text+0x61d): more undefined references to `vtable for MyWindow' follow
    17. collect2: ld returned 1 exit status
    18. make: *** [nonsense] Error 1
    To copy to clipboard, switch view to plain text mode 
    What's the reason of error here? I did a make clean and then make, it hasn't helped!
    Last edited by TheIndependentAquarius; 2nd May 2011 at 09:42.

  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: Qt: Signals and slots Error: undefined reference to `vtable for

    The output above clearly shows that there's no moc file generated.

    This probably means that you are using an old makefile.
    Remove all generated files before rebuilding again. It might be interesting to separate build and source directories. This way you can just delete the build directory and rebuild again.

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

    TheIndependentAquarius (2nd May 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Qt: Signals and slots Error: undefined reference to `vtable for

    thanks for responding, it is solved, some "moc" files hadn't generated because I didn't separate the code for the header, source and the main.cpp! and I couldn't find any error message related to the "moc" file in the above list of errors.

    and I mentioned before that I did a make clean, make distclean, but nothing helped.

  5. #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: Qt: Signals and slots Error: undefined reference to `vtable for

    Running make clean or make distclean is nothing to do with re-freshing an old makefile. For that you need to run qmake.

    Also, you don't have to seperate the code from the header as long as you inform qmake that you have done so by adding the line '#include "filename.moc"' to the end of your source file.

  6. The following user says thank you to squidge for this useful post:

    TheIndependentAquarius (2nd May 2011)

  7. #5
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: Qt: Signals and slots Error: undefined reference to `vtable for

    Thanks, I'll try that in my next example.

Similar Threads

  1. undefined reference to 'vtable ...'
    By GUIman in forum Newbie
    Replies: 5
    Last Post: 10th March 2011, 01:38
  2. Undefined reference to 'vtable for XXX'
    By Sheng in forum Qt Programming
    Replies: 4
    Last Post: 17th October 2008, 15:59
  3. Replies: 5
    Last Post: 7th November 2007, 14:46
  4. Error : undefined reference to `vtable for MyClass'
    By joseph in forum Qt Programming
    Replies: 23
    Last Post: 15th June 2007, 11:21
  5. Replies: 5
    Last Post: 14th February 2006, 23:43

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
  •  
Qt is a trademark of The Qt Company.