問(wèn)題描述
我在 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.
考慮以下代碼,這是產(chǎn)生錯(cuò)誤的最低限度:
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;
}
以上代碼片段導(dǎo)致以下鏈接器錯(cuò)誤:
The above code fragment causes the following linker errors:
在函數(shù)‘T’中:
未定義的對(duì)`vtable for T'的引用
undefined reference to `vtable for T'
未定義的對(duì)`vtable for T'的引用
undefined reference to `vtable for T'
在函數(shù)`~T'中:
未定義的對(duì)`vtable for T'的引用
undefined reference to `vtable for T'
未定義的對(duì)`vtable for T'的引用
undefined reference to `vtable for T'
如果我注釋掉包含 Q_OBJECT
的行,它編譯正常.我需要帶有 QGraphicsItem
的信號(hào)和插槽,所以我需要 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
.
代碼有什么問(wèn)題?謝謝.
What is wrong with the code? Thanks.
推薦答案
這是因?yàn)?MOC 生成的單元未包含在鏈接過(guò)程中.或者它根本沒(méi)有生成.我要做的第一件事是將類聲明放在一個(gè)單獨(dú)的頭文件中,也許構(gòu)建系統(tǒng)沒(méi)有掃描實(shí)現(xiàn)文件.
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.
還有一種可能是這個(gè)類曾經(jīng)不屬于Qt元對(duì)象系統(tǒng)(也就是說(shuō),它沒(méi)有Q_OBJECT或者可能根本沒(méi)有繼承自QObject),所以需要再次運(yùn)行qmake才能順序?yàn)?MOC 創(chuàng)建必要的規(guī)則.強(qiáng)制運(yùn)行 qmake 的最簡(jiǎn)單方法是對(duì)項(xiàng)目文件進(jìn)行一些微不足道的更改以更新其時(shí)間戳,例如添加然后刪除一些空格.或者,如果您使用的是 Qt Creator,則只需從項(xiàng)目上下文菜單中選擇運(yùn)行 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.
這篇關(guān)于Q_OBJECT 拋出“對(duì) vtable 的未定義引用"錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!