我在 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 的未定义引用"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!