Q_OBJECT 抛出“对 vtable 的未定义引用"错误

Q_OBJECT throwing #39;undefined reference to vtable#39; error(Q_OBJECT 抛出“对 vtable 的未定义引用错误)
本文介绍了Q_OBJECT 抛出“对 vtable 的未定义引用"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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’中:

未定义的对`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 的最简单方法是对项目文件进行一些微不足道的更改以更新其时间戳,例如添加然后删除一些空格.或者,如果您使用的是 Qt Creator,则只需从项目上下文菜单中选择运行 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. Or, if you're using Qt Creator, then just select "Run qmake" from the project context menu.

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Is it possible to connect a signal to a static slot without a receiver instance?(是否可以在没有接收器实例的情况下将信号连接到静态插槽?)
how to restart my own qt application?(如何重新启动我自己的 qt 应用程序?)
Where in Qt Creator do I pass arguments to a compiler?(在 Qt Creator 中,我在哪里将参数传递给编译器?)
Using a Qt-based DLL in a non-Qt application(在非 Qt 应用程序中使用基于 Qt 的 DLL)
Configuring the GCC compiler switches in Qt, QtCreator, and QMake(在 Qt、QtCreator 和 QMake 中配置 GCC 编译器开关)
Difference between creating object with () or without(使用 () 或不使用 () 创建对象的区别)