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();
}
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();
}