QT中的Q_OBJECT(对vtable xxx的未定义引用) [英] Q_OBJECT in QT (undefined reference to vtable xxx)
问题描述
我写了一个简单的测试,如下所示:
I wrote a simple test as follow:
#include <QCoreApplication>
#include <QObject>
class Foo : public QObject
{
Q_OBJECT
};
class foo1
{
};
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
Foo f;
return a.exec();
}
哪个给我一个错误:
错误:对"vtable for Foo"的未定义引用:
error: undefined reference to `vtable for Foo':
但是,当我将 Foo f
更改为 Foo f()
时,它可以正常执行.
所以我的问题是 f
和 f()
有什么区别?
However, when I change Foo f
to Foo f()
, it complies without any error.
So my question is what is the different between f
and f()
?
推荐答案
添加括号使其成为函数声明.实际上没有创建任何对象,因此不会发生错误.
Adding the parenthesis makes it a function declaration. No object is actually created, so the error doesn't happen.
Foo f; //declaration of variable of type Foo
Foo f(); //declaration of function taking no args and returning Foo
针对Foo的vtable的未定义引用
是因为您添加了对 Q_OBJECT
的调用,而没有再次运行qmake.一旦运行,该错误应消失.
The undefined reference to vtable for Foo
error is because you added a call to Q_OBJECT
without running qmake again. Once it's run, the error should disappear.
这篇关于QT中的Q_OBJECT(对vtable xxx的未定义引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!