Q_OBJECT抛出“未定义的引用vtable”错误 [英] Q_OBJECT throwing 'undefined reference to vtable' error

查看:1271
本文介绍了Q_OBJECT抛出“未定义的引用vtable”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7 Ultimate 32位上使用Qt 4.7.0(32位)的Qt Creator 2.0.1。

I'm using Qt Creator 2.0.1 with Qt 4.7.0 (32 bit) on Windows 7 Ultimate 32 bit.

请考虑以下代码,产生错误的最小值:

Consider the following code, which is a minimum to produce the error:

class T : public QObject, public QGraphicsItem
{
    Q_OBJECT

public:
    T() {}

    QRectF      boundingRect() const {return QRectF();}
    void        paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                      QWidget *widget) {}
};

int main()
{
    T t;
    return 0;
}

上述代码段导致以下链接器错误:

The above code fragment causes the following linker errors:


在函数`T'中:

In function `T':

未定义引用`vtable for T'

undefined reference to `vtable for T'

未定义引用`vtable for T'

undefined reference to `vtable for T'

在函数`〜T'中:

未定义引用`vtable for T'

undefined reference to `vtable for T'

未定义引用`vtable for T'

undefined reference to `vtable for T'

如果我注释掉包含 Q_OBJECT 的行,它编译正常。我需要信号和插槽与 QGraphicsItem 所以我需要 Q_OBJECT

If I comment out the line that contains Q_OBJECT, it compiles fine. I need signal and slots with QGraphicsItem so I need Q_OBJECT.

代码有什么问题?感谢。

What is wrong with the code? Thanks.

推荐答案

这是因为MOC生成的单元不包括在链接过程中。或者也许它不是生成的。我要做的第一件事是将类声明放在一个单独的头文件中,也许构建系统不扫描实现文件。

It is because the unit generated by MOC isn't included in the linking process. Or maybe it isn't generated at all. The first thing I'd do is to put the class declaration in a separate header file, perhaps the build system isn't scanning implementation files.

另一个可能性是类一次不属于Qt元对象系统(也就是说,它没有Q_OBJECT或者根本没有从QObject继承),所以qmake需要再次运行,以便为MOC创建必要的规则。强制qmake运行的最简单的方法是对项目文件进行一些无关紧要的更改,以更新其时间戳,如添加然后删除一些空格。

Another possibility is that the class in question once didn't belong to Qt meta object system (that is, it had no Q_OBJECT or maybe didn't inherit from QObject at all), so qmake needs to be run again in order to create the necessary rules for MOC. The easiest way to force qmake to be run is to make some insignificant changes to the project file to update its timestamp, like adding and then removing some white space.

这篇关于Q_OBJECT抛出“未定义的引用vtable”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆