c++ - 对 `vtable 的 undefined reference (最小示例)

标签 c++ qt

这可能是下一个拷贝,但我没有发现这段代码中的错误:

#include <qwt_plot.h>

class QLinePlot : public QwtPlot
{
    Q_OBJECT
public:
    QLinePlot(QWidget* parent = 0, Qt::WindowFlags flags = 0): QwtPlot(parent)
    {
    }

    ~QLinePlot()
    {
    }

};


int main( int argc, char **argv )
{
    QLinePlot * plot = new QLinePlot();
}

我删除了构建文件夹并再次运行 qmake,但没有任何变化。错误信息是

test.cpp:7: undefined reference to `vtable for QLinePlot'

最佳答案

您在文件末尾缺少 #include "test.moc":

// test.cpp
#include <qwt_plot.h>

class QLinePlot : public QwtPlot
{
    Q_OBJECT
public:
    using QwtPlot::QwtPlot;
};


int main( int argc, char **argv )
{
    QLinePlot plot;
}

#include "test.moc"

添加包含行后,您必须在项目上重新运行 qmake。

不过,您的例子可不是最小的。重现该问题所需要做的就是:

#include <QObject>
class Foo : public QObject {
  Q_OBJECT
  ~Foo() {}
}
int main() { Foo foo; }

关于c++ - 对 `vtable 的 undefined reference (最小示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40248761/

相关文章:

c++ - 使用 scanf 进行输入过滤

c++ - 将 DLL 调用到 VB.Net 中时出现未处理的 AccessViolationException 错误

c++ - Qt Creator 重构和 C++ 模板

C++ 和 Qt : is this a memory leak?(和一般问题)

c++ - 获取 QCheckBox 在 QTableWidget 中的单元格位置

c++ - 减少发布版本中非常大的静态库的大小

C++ - 使指向父类(super class)的指针与函数中的子类参数匹配

c++ - ADO:如何使用取消功能同步执行查询?

c++ - C/C++ 如何判断一个程序是否已经在运行?

c++ - 继承 QString 以添加更多功能?