PDA

View Full Version : undefined reference to vtable in Threads.



prasanth.nvs
6th February 2009, 05:40
In the following code i got compilation erros like below

/thread.cpp:20: undefined reference to `vtable for MyThread'

thread.cpp:6: undefined reference to `vtable for MyThread'

MyThread.h file is


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QObject>

class MyThread:public QThread
{
Q_OBJECT
public:
MyThread(QWidget* aParent);
~MyThread();

public slots:
void Finished();

protected:
void run();
};

#endif //MYTHREAD_H


and MyThread.cpp is

#include "thread.h"
#include <iostream.h>
#include <QMetaType>


MyThread::MyThread(QWidget* aParent)
{
qRegisterMetaType<MyThread*>("MyThread");
QObject::connect(this,SIGNAL(finished()),this,SLOT (Finished()));
if(!isRunning())
{
start();
}
else
{
return;
}
}

MyThread::~MyThread()
{

}

void MyThread::run()
{
cout <<"Hello :" << endl;
}


void MyThread::Finished()
{
cout << "In Finishes:" << endl;
}

and main.cpp is


int main(int argc,char* argv[])
{
QtopiaApplication app(argc,argv);

QWidget* lWidget = new QWidget;
lWidget->setGeometry(0,0,240,320);

MyThread* lThread = new MyThread(lWidget);

Widget->show();

return app.exec();
}


Now whts the problem in above code and wts the reason for that.

jpn
6th February 2009, 13:40
Is thread.h listed in .pro? Did you re-run qmake after adding Q_OBJECT?

jogeshwarakundi
19th February 2009, 07:59
In the following code i got compilation erros like below

/thread.cpp:20: undefined reference to `vtable for MyThread'

thread.cpp:6: undefined reference to `vtable for MyThread'

MyThread.h file is


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QObject>

class MyThread:public QThread
{
Q_OBJECT
public:
MyThread(QWidget* aParent);
~MyThread();

public slots:
void Finished();

protected:
void run();
};

#endif //MYTHREAD_H


and MyThread.cpp is

#include "thread.h"
#include <iostream.h>
#include <QMetaType>


MyThread::MyThread(QWidget* aParent)
{
qRegisterMetaType<MyThread*>("MyThread");
QObject::connect(this,SIGNAL(finished()),this,SLOT (Finished()));
if(!isRunning())
{
start();
}
else
{
return;
}
}

MyThread::~MyThread()
{

}

void MyThread::run()
{
cout <<"Hello :" << endl;
}


void MyThread::Finished()
{
cout << "In Finishes:" << endl;
}

and main.cpp is


int main(int argc,char* argv[])
{
QtopiaApplication app(argc,argv);

QWidget* lWidget = new QWidget;
lWidget->setGeometry(0,0,240,320);

MyThread* lThread = new MyThread(lWidget);

Widget->show();

return app.exec();
}


Now whts the problem in above code and wts the reason for that.

you are trying to call start() which in turn calls the void run() method from inside your MyThread constructor. this is not valid. before the object construction is complete, you cannot start calling the virtual methods of the object. this is a classic C++ error!

cheers!

Yeshwin
20th February 2009, 10:19
This problem occurs due to mistakes of Profile like Spellings of Variables.