PDA

View Full Version : mythread.cpp:(.text+0x89e): undefined reference to `vtable for ThreadDialog' error



ashukla
22nd September 2007, 11:12
Dear Viewer!
How to solve this problem!


mythread.o: In function `ThreadDialog::startOrStopThreadB()':
mythread.cpp:(.text+0x259): undefined reference to `ThreadDialog::staticMetaObject'
mythread.cpp:(.text+0x2b7): undefined reference to `ThreadDialog::staticMetaObject'
mythread.o: In function `ThreadDialog::startOrStopThreadA()':
mythread.cpp:(.text+0x389): undefined reference to `ThreadDialog::staticMetaObject'
mythread.cpp:(.text+0x3e7): undefined reference to `ThreadDialog::staticMetaObject'
mythread.o: In function `ThreadDialog::ThreadDialog(QWidget*)':
mythread.cpp:(.text+0x4ae): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0x4b5): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0x57b): undefined reference to `ThreadDialog::staticMetaObject'
mythread.cpp:(.text+0x5de): undefined reference to `ThreadDialog::staticMetaObject'
mythread.cpp:(.text+0x641): undefined reference to `ThreadDialog::staticMetaObject'
mythread.o: In function `ThreadDialog::ThreadDialog(QWidget*)':
mythread.cpp:(.text+0x89e): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0x8a5): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0x96b): undefined reference to `ThreadDialog::staticMetaObject'
mythread.cpp:(.text+0x9ce): undefined reference to `ThreadDialog::staticMetaObject'
mythread.cpp:(.text+0xa31): undefined reference to `ThreadDialog::staticMetaObject'
mythread.o: In function `main':
mythread.cpp:(.text+0xcba): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0xcc1): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0xda2): undefined reference to `vtable for ThreadDialog'
mythread.cpp:(.text+0xda9): undefined reference to `vtable for ThreadDialog'
collect2: ld returned 1 exit status
make: *** [mythread] Error 1


The code is as follows;

/*mythread.h*/
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QtGui>
#include <QString>

class MyThread : public QThread
{
Q_OBJECT
volatile bool stopped;
QString msg;

public:
MyThread(QObject *parent=0);
void run(); // this is virtual method, we must implement it in our subclass of QThread
void setMessage(const QString &message);
void stop();

};
#endif

/*mythread.cpp*/
#include "mythread.h"
#include <iostream.h>

int i=0;
MyThread::MyThread(QObject *parent)
: QThread(parent)
{
stopped = false;

//run();
}

void MyThread::run()
{
while (!stopped)
{

cerr << qPrintable(msg)<< endl;
msleep(1000);
//if(++i==10)
//stop();
}




}

void MyThread::stop()
{
stopped = true;
}
void MyThread::setMessage(const QString &message)
{
msg=message;
}

class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent = 0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
MyThread threadA;
MyThread threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};

ThreadDialog::ThreadDialog(QWidget *parent)
: QDialog(parent)
{
threadA.setMessage("Anurag");
threadB.setMessage("Sonu");
threadAButton = new QPushButton(tr("Start A"));
threadBButton = new QPushButton(tr("Start B"));
quitButton = new QPushButton(tr("Quit"));
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()),
this, SLOT(startOrStopThreadA()));
connect(threadBButton, SIGNAL(clicked()),
this, SLOT(startOrStopThreadB()));

}

void ThreadDialog::startOrStopThreadA()
{
if (threadA.isRunning()) {
threadA.stop();
threadAButton->setText(tr("Start A"));
} else {
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()) {
threadB.stop();
threadBButton->setText(tr("Start B"));
} else {
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}

void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}

int main(int argc, char **argv)
{
QApplication app(argc,argv);
ThreadDialog t;
return app.exec();
}

caduel
22nd September 2007, 12:11
Make sure that your .pro file is uptodate.
(Esp. check that all your .h files are listed after HEADERS += ...;
and your .cpp files after SOURCES += ...)

Run qmake again it should work.

(This error almost always indicates to one of the following:
* forgotten Q_OBJECT in class declaration
* missing files in .pro file
* forgotten to update makefiles
Resulting in: moc not being run; no Qt-code being generated to implement signals and slots... thus the undefined references.)

HTH
Christoph

ashukla
22nd September 2007, 12:24
I have not forgotten Q_OBJECT macro.
No files is missing to include in .pro files.
moc files is created as well.
but application is not running.

jpn
22nd September 2007, 13:52
Is ThreadDialog declared in main.cpp? If so, add this:


#include "main.moc"

at the end of main.cpp and re-run qmake.

babu198649
7th November 2007, 12:53
hi
clean all the files created by qmake and make and compile freshly whenever u make a change that is realated with virtual table . i have accidently discovered this method ,but i dont know why this works.


i am using eclipse for qt and hence i dont have trouble in making and cleaning,
bye;)

jacek
7th November 2007, 14:46
clean all the files created by qmake and make and compile freshly whenever u make a change that is realated with virtual table . i have accidently discovered this method ,but i dont know why this works.
All you need is to re-run qmake and you have to do this every time you add or remove Q_OBJECT (or Q_GADGET) macro.