Home >Backend Development >C++ >Why Am I Getting a \'Q_OBJECT Linking Error: \'undefined reference to vtable\'\' in My Qt Application?
Consider the following Qt code snippet:
<code class="cpp">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; }</code>
When compiling this code, you may encounter linker errors like:
undefined reference to `vtable for T' undefined reference to `vtable for T' ...
Solution:
This error typically occurs when the MOC-generated unit for your class is not included in the linking process. The MOC unit contains meta-information about the class, including its Q_OBJECT macro.
Possible Causes and Fixes:
The above is the detailed content of Why Am I Getting a \'Q_OBJECT Linking Error: \'undefined reference to vtable\'\' in My Qt Application?. For more information, please follow other related articles on the PHP Chinese website!