Q_OBJECT throwing #39;undefined reference to vtable#39; error(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 的未定义引用"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Q_OBJECT 抛出“对 vtable 的未定义引用"错误


基础教程推荐
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17