简体   繁体   English

未定义引用`vtable for MainWindow'

[英]undefined reference to `vtable for MainWindow'

I'm following Qt's guide for using a QPushButton . 我正在关注使用QPushButton Qt指南

I did exactly as the guide suggests, but I'm getting a compilation error: 我完全按照指南的建议,但我收到了编译错误:

./src/mainwindow.o: In function `MainWindow::MainWindow(QWidget*)':
mainwindow.cpp:(.text+0x1d): undefined reference to `vtable for MainWindow'
./src/mainwindow.o:mainwindow.cpp:(.text+0x25): more undefined references to `vtable for MainWindow' follow
collect2: error: ld returned 1 exit status
make: *** [HelloWorldProj] Error 1

I tried adding a destructor: 我尝试添加析构函数:

~MainWindow(){};

but the problem persisted. 但问题仍然存在。

I have no virtual functions declared, except one function inside QMainWindow (the class I'm inheriting from): 我没有声明虚函数,除了QMainWindow一个函数(我继承的类):

virtual QMenu *createPopupMenu();

Should this be defined in my class? 这应该在我班上定义吗?

You need to set CMAKE_AUTOMOC to ON in our CMake file. 您需要在我们的CMake文件中将CMAKE_AUTOMOC设置为ON

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

"The AUTOMOC target property controls whether cmake inspects the C++ files in the target to determine if they require moc to be run, and to create rules to execute moc at the appropriate time." “AUTOMOC目标属性控制cmake是否检查目标中的C ++文件,以确定它们是否需要运行moc,以及创建在适当的时间执行moc的规则。” - Reference - 参考

Try running qmake on the project and then do a rebuild. 尝试在项目上运行qmake,然后进行重建。

Your example is broken (I will update it shortly if possible) 您的示例已损坏(如果可能,我会尽快更新)

Create an empty file and name it PushButtonExample.pro and add the following: 创建一个空文件并将其命名为PushButtonExample.pro并添加以下内容:

QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = PushButtonExample
TEMPLATE = app

SOURCES += main.cpp \
    mainwindow.cpp

HEADERS  += mainwindow.h

Then run qmake on that file, then make . 然后在该文件上运行qmake ,然后make

I also recommend that you download Qt Creator and use that as your IDE when building Qt project. 我还建议您下载Qt Creator并在构建Qt项目时将其用作IDE。 Most Qt installs also install the Qt Creator IDE and it has some nice examples and wizards to create new projects. 大多数Qt安装也安装了Qt Creator IDE,它有一些很好的例子和向导来创建新项目。

Make sure all 4 files are in the same folder 确保所有4个文件都在同一个文件夹中

main.cpp
mainwindow.cpp
mainwindow.h
PushButtonExample.pro

In command line, navigate into that folder and run qmake 在命令行中,导航到该文件夹​​并运行qmake

qmake PushButtonExample.pro

This should create the following file in the same folder 这应该在同一文件夹中创建以下文件

Makefile

Then run make 然后运行make

This should build the example, and finally run the application: 这应该构建示例,最后运行应用程序:

./PushButtonExample

(I am also updating the wiki page) (我也在更新维基页面)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM