[英]undefined reference to `vtable for MainWindow'
我正在關注使用QPushButton
Qt指南 。
我完全按照指南的建議,但我收到了編譯錯誤:
./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
我嘗試添加析構函數:
~MainWindow(){};
但問題仍然存在。
我沒有聲明虛函數,除了QMainWindow
一個函數(我繼承的類):
virtual QMenu *createPopupMenu();
這應該在我班上定義嗎?
您需要在我們的CMake文件中將CMAKE_AUTOMOC
設置為ON
。
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
“AUTOMOC目標屬性控制cmake是否檢查目標中的C ++文件,以確定它們是否需要運行moc,以及創建在適當的時間執行moc的規則。” - 參考
嘗試在項目上運行qmake,然后進行重建。
您的示例已損壞(如果可能,我會盡快更新)
創建一個空文件並將其命名為PushButtonExample.pro並添加以下內容:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = PushButtonExample
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
然后在該文件上運行qmake
,然后make
。
我還建議您下載Qt Creator並在構建Qt項目時將其用作IDE。 大多數Qt安裝也安裝了Qt Creator IDE,它有一些很好的例子和向導來創建新項目。
確保所有4個文件都在同一個文件夾中
main.cpp
mainwindow.cpp
mainwindow.h
PushButtonExample.pro
在命令行中,導航到該文件夾並運行qmake
qmake PushButtonExample.pro
這應該在同一文件夾中創建以下文件
Makefile
然后運行make
這應該構建示例,最后運行應用程序:
./PushButtonExample
(我也在更新維基頁面)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.