PDA

View Full Version : [SOLVED] Qt: Signals and slots Error: undefined reference to `vtable for



TheIndependentAquarius
2nd May 2011, 08:57
Following example from this link: http://developer.kde.org/documentation/books/kde-2.0-development/ch03lev1sec3.html



#include <QObject>
#include <QPushButton>
#include <iostream>
using namespace std;

class MyWindow : public QWidget
{
Q_OBJECT // Enable slots and signals
public:
MyWindow();
private slots:
void slotButton1();
void slotButton2();
void slotButtons();
private:
QPushButton *button1;
QPushButton *button2;
};

MyWindow :: MyWindow() : QWidget()
{
// Create button1 and connect button1->clicked() to this->slotButton1()
button1 = new QPushButton("Button1", this);
button1->setGeometry(10,10,100,40);
button1->show();
connect(button1, SIGNAL(clicked()), this, SLOT(slotButton1()));

// Create button2 and connect button2->clicked() to this->slotButton2()
button2 = new QPushButton("Button2", this);
button2->setGeometry(110,10,100,40);
button2->show();
connect(button2, SIGNAL(clicked()), this, SLOT(slotButton2()));

// When any button is clicked, call this->slotButtons()
connect(button1, SIGNAL(clicked()), this, SLOT(slotButtons()));
connect(button2, SIGNAL(clicked()), this, SLOT(slotButtons()));
}

// This slot is called when button1 is clicked.
void MyWindow::slotButton1()
{
cout << "Button1 was clicked" << endl;
}

// This slot is called when button2 is clicked
void MyWindow::slotButton2()
{
cout << "Button2 was clicked" << endl;
}

// This slot is called when any of the buttons were clicked
void MyWindow::slotButtons()
{
cout << "A button was clicked" << endl;
}

int main ()
{
MyWindow a;
}

results in:



[13:33:45 Mon May 02] ~/junkPrograms/src/nonsense $ls
nonsense.pro signalsSlots.cpp

[13:33:46 Mon May 02] ~/junkPrograms/src/nonsense $qmake
[13:33:50 Mon May 02] ~/junkPrograms/src/nonsense $make
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
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
signalsSlots.o: In function `MyWindow::MyWindow()':
signalsSlots.cpp:(.text+0x1a2): undefined reference to `vtable for MyWindow'
signalsSlots.cpp:(.text+0x1aa): undefined reference to `vtable for MyWindow'
signalsSlots.o: In function `MyWindow::MyWindow()':
signalsSlots.cpp:(.text+0x3e2): undefined reference to `vtable for MyWindow'
signalsSlots.cpp:(.text+0x3ea): undefined reference to `vtable for MyWindow'
signalsSlots.o: In function `main':
signalsSlots.cpp:(.text+0x614): undefined reference to `vtable for MyWindow'
signalsSlots.o:signalsSlots.cpp:(.text+0x61d): more undefined references to `vtable for MyWindow' follow
collect2: ld returned 1 exit status
make: *** [nonsense] Error 1
What's the reason of error here? I did a make clean and then make, it hasn't helped!

tbscope
2nd May 2011, 09:19
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.

TheIndependentAquarius
2nd May 2011, 09:34
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! http://static.linuxquestions.org/questions/images/smilies/mad.gif 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.

squidge
2nd May 2011, 10:47
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.

TheIndependentAquarius
2nd May 2011, 10:51
Thanks, I'll try that in my next example.