C++ 对 vtable 的未定义引用。尝试编译 Qt 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1552069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined reference to vtable. Trying to compile a Qt project
提问by TheFuzz
I'm using Code::Blocks8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:
我正在使用Code::Blocks8.02 和 mingw 5.1.6 编译器。编译 Qt 项目时出现此错误:
C:\Documents and Settings\The Fuzz\Desktop\GUI\App_interface.cpp|33|undefined reference to `vtable for AddressBook'
C:\Documents and Settings\The Fuzz\Desktop\GUI\App_interface.cpp|33|未定义对“地址簿vtable”的引用
File AddressBook.h:
文件地址簿.h:
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class AddressBook : public QWidget
{
Q_OBJECT
public:
AddressBook(QWidget *parent = 0);
private:
QLineEdit *nameLine;
QTextEdit *addressText;
};
#endif
File AddressBook.cpp:
文件地址簿.cpp:
#include <QtGui>
#include "addressbook.h"
AddressBook::AddressBook(QWidget *parent)
: QWidget(parent)
{
QLabel *nameLabel = new QLabel(tr("Name:"));
nameLine = new QLineEdit;
QLabel *addressLabel = new QLabel(tr("Address:"));
addressText = new QTextEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(nameLabel, 0, 0);
mainLayout->addWidget(nameLine, 0, 1);
mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
mainLayout->addWidget(addressText, 1, 1);
setLayout(mainLayout);
setWindowTitle(tr("Simple Address Book"));
}
采纳答案by blwy10
Warning: Do not do this if you already have a .pro file - you'll lose it!
警告:如果您已经有 .pro 文件,请不要这样做 - 您会丢失它!
In order to automatically ensure that all moc cpp files are generated, you can get qmake to automatically generate a .pro file for you instead of writing one yourself.
为了自动保证生成所有的moc cpp文件,你可以让qmake自动为你生成一个.pro文件,而不是自己写一个。
Run
跑
qmake -project
in the project directory, and qmake will scan your directory for all C++ headers and source files to generate moc cpp files for.
在项目目录中,qmake 将扫描您的目录以查找所有 C++ 头文件和源文件,以生成 moc cpp 文件。
回答by Anurag Verma
When using Qt Creator:
使用 Qt Creator 时:
- Build → Run qmake
- Build → Rebuild All
- 构建 → 运行 qmake
- 构建 → 全部重建
回答by Caleb Huitt - cjhuitt
The problem is almost certainly that you are not compiling or not linking in the generated moc_AddressBook.cpp file. (It should have been generated for you -- you are running Qt's moc
on your code before compiling, right?)
问题几乎可以肯定是您没有在生成的 moc_AddressBook.cpp 文件中进行编译或链接。(它应该是为您生成的——您moc
在编译之前在代码上运行 Qt ,对吗?)
To answer a little more thoroughly, the Q_OBJECT
macro signals Qt's moc
tool to create an extra implementation file that contains the code necessary to support QObject
's meta-information system. If you had any signals or slots, it would do a few things for those as well.
为了更彻底地回答,Q_OBJECT
宏向 Qt 的moc
工具发出信号,以创建一个额外的实现文件,其中包含支持QObject
的元信息系统所需的代码。如果您有任何信号或插槽,它也会为它们做一些事情。
An alternative solution might be to remove the Q_OBJECT
macro. You probably don't want to do this, but it would help the immediate problem, and it isn't strictly necessary with the code that you've presented.
另一种解决方案可能是删除Q_OBJECT
宏。您可能不想这样做,但它会帮助解决眼前的问题,而且对于您提供的代码来说,这并不是绝对必要的。
Also, I would note that your line:
另外,我会注意到你的线路:
#include "addressbook.h"
Should probably be:
应该是:
#include "AddressBook.h"
based on how you presented the filenames in the question.
根据您在问题中显示文件名的方式。
回答by Jeremy Friesner
Assuming you are using qmake to generate your Makefile, be sure that AddressBook.h is specified in your .pro file's HEADERS's variable, e.g.
假设您使用 qmake 生成 Makefile,请确保在 .pro 文件的 HEADERS 变量中指定了 AddressBook.h,例如
HEADERS = AddressBook.h
回答by Jesse Victors
I got this while using pure virtual functions. For example,
我在使用纯虚函数时得到了这个。例如,
virtual void process();
gave this error, while
给出了这个错误,而
virtual void process() = 0;
made it go away.
让它消失了。
For anyone who's Googling this problem, check that all virtual functions are defined. (via " = 0" or a full definition in the source file) I'm using Netbeans with the MinGW compiler.
对于在 Google 上搜索此问题的任何人,请检查是否已定义所有虚函数。(通过“= 0”或源文件中的完整定义)我将 Netbeans 与 MinGW 编译器一起使用。
回答by Dumisani Kunene
For CMake projects, set CMAKE_AUTOMOC to ON, this fixed my problem.
对于 CMake 项目,将 CMAKE_AUTOMOC 设置为 ON,这解决了我的问题。
#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
回答by yolo
deleted the build folder, restarted Qt Creator and it worked
删除了构建文件夹,重新启动了 Qt Creator 并且它工作了
回答by yan bellavance
I had the same problem but as soon as I defined my constructor in the header file instead of the .cpp the error disappeared. Also the corresponding moc file was missing in the file system and in the Makefile section"compiler_moc_header_make_all" . I ran a qmake then finally everything built with succes. I went to check the Makefile and it was there now.
我遇到了同样的问题,但是一旦我在头文件而不是 .cpp 中定义了我的构造函数,错误就消失了。此外,文件系统和 Makefile 部分“compiler_moc_header_make_all”中也缺少相应的 moc 文件。我跑了一个 qmake 然后最后一切都成功了。我去检查了 Makefile,它现在就在那里。
回答by user281754
I come to the same problem, rebuild the project never update the Makefile, I remove the Makefile and rebuild , the the problem is gone. ps: run 'make' from command line may give you detail information than the IDE, and helpful to get the real problem.
我遇到了同样的问题,重建项目从不更新 Makefile,我删除了 Makefile 并重建,问题消失了。ps:从命令行运行'make'可能会为您提供比IDE更详细的信息,并有助于解决真正的问题。
回答by agus
One cause is when you declare a virtual functions in a class and you don't define their body.
一个原因是当您在类中声明虚函数并且没有定义它们的主体时。