qt错误:未定义对"vtable for Thread"的引用 [英] qt error: undefined reference to `vtable for Thread'
本文介绍了qt错误:未定义对"vtable for Thread"的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有代码:
#include <iostream>
#include <QThread>
#include <unistd.h>
#include <stdlib.h>
#include <QApplication>
using std::cerr;
using std::endl;
class Thread : public QThread
{
Q_OBJECT
public:
Thread();
~Thread();
void setMessage(const QString &_message);
void stop();
protected:
void run();
private:
QString message;
volatile bool stopped;
};
Thread::Thread()
{
stopped = false;
run();
}
Thread::~Thread()
{
}
void Thread::run()
{
while(!stopped){
cerr << qPrintable(message);
sleep(1);
}
stopped = false;
cerr << endl;
}
void Thread::stop()
{
stopped = true;
}
void Thread::setMessage(const QString &_message)
{
message = _message;
}
int main(int argc,char *argv[])
{
QApplication app(argc, argv);
Thread *A,*B;
A = new Thread();
B = new Thread();
A->setMessage("Thread A\n");
B->setMessage("Thread B\n");
//.run();
//.run();
sleep(10);
A->stop();
B->stop();
return 0;
}
我有错误
g++ -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-O1 -o tmp main.o -L/usr/lib -lQtGui -lQtCore -lpthread
main.o: In function `Thread::~Thread()':
main.cpp:(.text+0xa): undefined reference to `vtable for Thread'
main.o: In function `Thread::Thread()':
main.cpp:(.text+0x1da): undefined reference to `vtable for Thread'
collect2: ld returned 1 exit status
make: *** [tmp] Error 1
推荐答案
您必须使用moc生成标头.这可以通过Qt构建系统自动完成.而不是直接使用gcc,您应该使用qmake文件.
You must generate a header with moc. This can be done automatically with the Qt build system. Instead of using gcc directly, you should use a qmake file.
另外,您可能应该将声明和代码分为头文件和cpp文件.
Also you should probably separate the declaration and code into header file and cpp file.
以下是moc所做的描述: http://doc.qt.nokia.com/latest/moc.html
Here is a description of what moc does: http://doc.qt.nokia.com/latest/moc.html
还有一个关于stackoverflow的类似问题(带有答案):对vtable的未定义引用.尝试编译Qt项目
And a similar question (with answers) here on stackoverflow: Undefined reference to vtable. Trying to compile a Qt project
这篇关于qt错误:未定义对"vtable for Thread"的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文