gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:28:55 25 4
gpt4 key购买 nike

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

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com