【问题标题】:Q_OBJECT in QT (undefined reference to vtable xxx) [duplicate]QT中的Q_OBJECT(未定义对vtable xxx的引用)[重复]
【发布时间】:2016-03-12 05:03:43
【问题描述】:

我写了一个简单的测试如下:

#include <QCoreApplication>
#include <QObject>

class Foo : public QObject
{
   Q_OBJECT
};

class foo1
{
};

int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
Foo f;
return a.exec();
}

这给了我一个错误:

错误:未定义对 `vtable for Foo' 的引用:

但是,当我将 Foo f 更改为 Foo f() 时,它没有任何错误。
所以我的问题是ff() 有什么区别?

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    添加括号使其成为函数声明。没有实际创建对象,因此不会发生错误。

    Foo f; //declaration of variable of type Foo
    
    Foo f(); //declaration of function taking no args and returning Foo
    

    undefined reference to vtable for Foo 错误是因为您添加了对Q_OBJECT 的调用而没有再次运行 qmake。运行后,错误应该会消失。

    【讨论】:

    • 我已经尝试清理项目并再次运行qmake,但是当我使用Foo f时仍然出现错误
    【解决方案2】:

    这个错误是由于在main.cpp中声明了类扩展QObject,所以 只需在您的主要方法之前(或之后)添加 #include "main.moc" 行。 但是下次为 QObject 扩展类定义一个单独的 h/cpp 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 2011-12-01
      • 1970-01-01
      • 2011-07-27
      • 2021-11-30
      相关资源
      最近更新 更多