[英]qt error: undefined reference to `vtable for Thread'
I have the code: 我有代码:
#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;
}
and i have error 我有错误
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
You must generate a header with moc. 您必须使用moc生成标头。 This can be done automatically with the Qt build system.
这可以使用Qt构建系统自动完成。 Instead of using gcc directly, you should use a qmake file.
您应该使用qmake文件而不是直接使用gcc。
Also you should probably separate the declaration and code into header file and cpp file. 你也应该把声明和代码分成头文件和cpp文件。
Here is a description of what moc does: http://doc.qt.nokia.com/latest/moc.html 以下是moc所做的描述: http : //doc.qt.nokia.com/latest/moc.html
And a similar question (with answers) here on stackoverflow: Undefined reference to vtable. stackoverflow上有一个类似的问题(有答案): 对vtable的未定义引用。 Trying to compile a Qt project
试图编译一个Qt项目
You need to have a line at the bottom of your source file: 您需要在源文件的底部有一行:
#include "main.moc"
That's because the declaration of class Thread
isn't in a header - it's in a .cpp file. 那是因为
class Thread
的声明不在标题中 - 它在.cpp文件中。 So by default moc
won't run on it. 所以默认情况下
moc
不会在它上面运行。 Adding the line does two things: 添加该行有两个作用:
qmake
and moc
that moc
has to process the .cpp file qmake
和moc
发出信号,表示moc
必须处理.cpp文件 moc
generates to be pulled in by the compile step moc
生成的东西被编译步骤拉入 So after adding that line you'll need to rerun qmake
so it can update the makefiles to cause main.moc
to be generated. 因此,在添加该行之后,您需要重新运行
qmake
以便它可以更新makefile以生成main.moc
。
Normally, moc
runs against header files and creates .cpp files that get included in the build ( qmake
sees to this). 通常,
moc
针对头文件运行并创建包含在构建中的.cpp文件( qmake
看到这个)。 This 'trick' causes moc
to also be run on the .cpp filein question (and to have the moc
generated code compiled in). 这个'技巧'导致
moc
也在问题上运行在.cpp文件上(并且编译了moc
生成的代码)。
An alternative to including main.moc
at the end of main.cpp
is to move the definition of class Thread
to a .h header file and #include
that. 在
main.cpp
的末尾包含main.moc
的替代方法是将class Thread
的定义移动到.h头文件和#include
。 If the definition is in a header qmake
and moc
should handle things automatically. 如果定义在头文件中,则
qmake
和moc
应自动处理事物。
I think that the qmake system requires that your header file include directly. 我认为 qmake系统要求您的头文件直接包含。 Mine was failing to generate a moc until I added that include.
在我添加include之前,我没有生成moc。
Simple add string to .pro file. 简单地将字符串添加到.pro文件。 This was helping for me in same problem
这对我有同样的问题
INSTALLS += target
See queudcustomtype example. 请参阅queudcustomtype示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.