[英]undefined reference to vtable when compiling a Qt hello World
I was following this tutorial where I designed a simple window with a single button and try to trigger it. 我按照本教程设计了一个带有单个按钮的简单窗口,然后尝试触发它。 When I compile it I get an error and I don't really understand where it comes from.
当我编译它时,我得到一个错误,我真的不知道它从哪里来。 When I compile a simple program that works, but now when I try to create my own gui using QDesginer it doesn't work anymore.
当我编译一个可以运行的简单程序时,但是现在当我尝试使用QDesginer创建自己的GUI时,该程序将不再起作用。 Usually the
vtable
error means that some virtual function is not implemented, but I don't see wheer this should result from. 通常,
vtable
错误表示未实现某些虚函数,但我不认为这应由什么原因引起。 I have looked at similar questions here like QT C++ error: undefined reference to `vtable for appprinter' or Qt with codeblocks - undefined reference to vtable but this doesn't really help. 我在这里看过类似的问题,例如QT C ++错误:未定义对`vtable for appprinter'的 引用或带有代码块的 Qt- 未定义对vtable的引用,但这并没有真正的帮助。
main.cpp
#include <QtWidgets/qapplication.h>
#include <QtWidgets/qpushbutton.h>
#include "main_frame.h"
class TestMainFrame : public QFrame
{
Q_OBJECT
public:
explicit TestMainFrame(QWidget *parent = 0);
~TestMainFrame();
private slots:
void onTest();
private:
Ui::MainFrameGUI *ui;
};
TestMainFrame::TestMainFrame(QWidget *parent) :
QFrame(parent),
ui(new Ui::MainFrameGUI)
{
ui->setupUi(this);
}
TestMainFrame::~TestMainFrame()
{
delete ui;
}
void TestMainFrame::onTest()
{
printf("Test\n");
}
int main( int argc, char **argv )
{
QApplication a(argc, argv);
TestMainFrame w;
w.show();
return a.exec();
}
main_frame.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainFrameGUI</class>
<widget class="QFrame" name="MainFrameGUI">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>295</width>
<height>77</height>
</rect>
</property>
<property name="windowTitle">
<string>Testbutton</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="mButton">
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>mButton</sender>
<signal>clicked()</signal>
<receiver>MainFrameGUI</receiver>
<slot>onTest()</slot>
<hints>
<hint type="sourcelabel">
<x>303</x>
<y>38</y>
</hint>
<hint type="destinationlabel">
<x>303</x>
<y>38</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>onTest()</slot>
</slots>
</ui>
build.log
d:\opt\qt-5.2.1\bin\uic.exe -g cpp -o d:\src\c\QTFrameQML\main_frame.h d:\src\c\QTFrameQML\main_frame.ui
g++.exe -std=c++11 -Wall -g -ID:\opt\qt-5.2.1\include -c d:\src\c\QTFrameQML\main.cpp -o Debug\obj\main.o
g++.exe -LD:\opt\qt-5.2.1\lib -o Debug\QTFrameQML.exe Debug\obj\main.o -lgdi32 -luser32 -lkernel32 -lcomctl32 -lQt5Core -lQt5Gui -lGLESv2d -lQt5Widgetsd -mwindows
Debug\obj\main.o: In function `ZN13TestMainFrameC2EP7QWidget':
d:/src/c/QTFrameQML/main.cpp:23: undefined reference to `vtable for TestMainFrame'
d:/src/c/QTFrameQML/main.cpp:23: undefined reference to `vtable for TestMainFrame'
Debug\obj\main.o: In function `ZN13TestMainFrameD2Ev':
d:/src/c/QTFrameQML/main.cpp:28: undefined reference to `vtable for TestMainFrame'
d:/src/c/QTFrameQML/main.cpp:28: undefined reference to `vtable for TestMainFrame'
collect2.exe: error: ld returned 1 exit status
请勿在cpp文件中使用Q_OBJECT宏,除非您要手动运行moc ...只需将类定义移至main.h,将其包含在SOURCES中并重新运行qmake-> works
this is often an dependency issue where the moc-compiler where not invoked due to an already existing object file. 这通常是一个依赖性问题,其中由于已经存在目标文件而未调用moc编译器。 try to distclean your environment and start qmake again.
尝试清理环境,然后重新启动qmake。 hope that helps
希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.