对 vtable 的未定义引用。尝试编译 Qt 项目

发布于 2024-08-07 15:13:48 字数 1303 浏览 3 评论 0原文

我正在使用 Code::Blocks 8.02 和 mingw 5.1.6 编译器。编译 Qt 项目时出现此错误:

C:\Documents and Settings\The Fuzz\Desktop\GUI\App_interface.cpp|33|未定义 引用“AddressBook 的 vtable”

文件 AddressBook.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

文件 AddressBook.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"));
}

I'm using Code::Blocks 8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:

C:\Documents and Settings\The
Fuzz\Desktop\GUI\App_interface.cpp|33|undefined
reference to `vtable for AddressBook'

File AddressBook.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:

#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"));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(21

死开点丶别碍眼 2024-08-14 15:13:48

使用 Qt Creator 时:

  1. 构建 → 运行 qmake
  2. 构建 → 全部重建

When using Qt Creator:

  1. Build → Run qmake
  2. Build → Rebuild All
第几種人 2024-08-14 15:13:48

警告:如果您已有 .pro 文件,请不要执行此操作 - 您会丢失它!

为了自动确保生成所有 moc cpp 文件,您可以让 qmake 自动生成 .pro 文件。 pro 文件为您提供,而不是您自己编写。

运行,qmake 将扫描您的目录中的所有 C++ 头文件和源文件以生成 moc cpp 文件。

qmake -project

在项目目录中

Warning: Do not do this if you already have a .pro file - you'll lose it!

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.

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.

久随 2024-08-14 15:13:48

问题几乎可以肯定是您没有在生成的 moc_AddressBook.cpp 文件中进行编译或链接。 (它应该已经为您生成了——您在编译之前在代码上运行 Qt 的 moc,对吧?)

为了更彻底地回答一点,Q_OBJECT 宏发出 Qt 的信号moc 工具,用于创建额外的实现文件,其中包含支持 QObject 元信息系统所需的代码。如果你有任何信号或槽,它也会为这些做一些事情。

另一种解决方案可能是删除 Q_OBJECT 宏。您可能不想这样做,但这将有助于解决眼前的问题,并且对于您所提供的代码来说这并不是绝对必要的。

另外,我会注意到您的行:

#include "addressbook.h"

应该是:

#include "AddressBook.h"

基于您在问题中呈现文件名的方式。

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?)

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.

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.

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.

后来的我们 2024-08-14 15:13:48

假设您使用 qmake 生成 Makefile,请确保在 .pro 文件的 HEADERS 变量中指定了 AddressBook.h,例如

HEADERS = AddressBook.h

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.

HEADERS = AddressBook.h
半世蒼涼 2024-08-14 15:13:48

对于 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)

For CMake projects, set CMAKE_AUTOMOC to ON, this fixed my problem.

#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
木槿暧夏七纪年 2024-08-14 15:13:48

我在使用纯虚函数时得到了这个。例如,

virtual void process();

给出了这个错误,同时

virtual void process() = 0;

让它消失。

对于任何在谷歌上搜索此问题的人,请检查所有虚拟函数是否已定义。 (通过“= 0”或源文件中的完整定义)
我将 Netbeans 与 MinGW 编译器一起使用。

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.

手心的温暖 2024-08-14 15:13:48

我使用 CLion 遇到了同样的问题,并通过将这些行添加到 CMakeLists.txt 来解决它,

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

请参阅 https://www.jetbrains.com/help/clion/qt-tutorial.html#qt-setup-in-clion

I was running into the same problem using CLion, and solved it by adding these lines to CMakeLists.txt

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

See https://www.jetbrains.com/help/clion/qt-tutorial.html#qt-setup-in-clion

悍妇囚夫 2024-08-14 15:13:48

我遇到了同样的问题,但是一旦我在头文件而不是 .cpp 中定义了构造函数,错误就消失了。文件系统和 Makefile 部分“compiler_moc_header_make_all”中也缺少相应的 moc 文件。我运行了 qmake,最后一切都成功构建了。我去检查了 Makefile,它现在就在那里。

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.

凡间太子 2024-08-14 15:13:48

删除build文件夹,重新启动Qt Creator,它就工作了

deleted the build folder, restarted Qt Creator and it worked

初心 2024-08-14 15:13:48

我遇到了同样的问题,重建项目从不更新 Makefile,我删除 Makefile 并重建,问题就消失了。
ps:从命令行运行“make”可能会为您提供比 IDE 更详细的信息,并且有助于解决真正的问题。

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.

心凉 2024-08-14 15:13:48

原因之一是当您在类中声明虚拟函数但没有定义它们的函数体时。

One cause is when you declare a virtual functions in a class and you don't define their body.

迷爱 2024-08-14 15:13:48

我在尝试使用受保护的虚拟函数时遇到了同样的问题。有两件事奏效了。

  1. void process(); 更改为 void process() = 0;
  2. process() 设为公共而不是私有

I had the same problem trying to use a protected virtual function. Two things worked.

  1. Changing void process(); to void process() = 0;
  2. Making process() public instead of private
没︽人懂的悲伤 2024-08-14 15:13:48

就我而言,“重建全部”还不够,我必须删除构建目录并进行“重建全部” - 然后它就起作用了!

In my case Rebuild All was not enough, I had to delete build directory and make Rebuild All - then it worked!

心是晴朗的。 2024-08-14 15:13:48

只需为您的项目运行 qmake 即可。通过右键单击项目名称并单击运行 qmake 可以轻松完成此操作。

Simply Run qmake for your project. This can easily be done by right-clicking on the name of your project and clicking on Run qmake.

許願樹丅啲祈禱 2024-08-14 15:13:48

只需清除项目然后重建它即可!

Just clear the project and then rebuild it!

殤城〤 2024-08-14 15:13:48

转到 .pro 文件并确保 .h 文件前面有“include”。
标头 += include/file.h \
包含/file2.h

Go to .pro file and make sure .h file has 'include' before it.
HEADERS += include/file.h \
include/file2.h

远昼 2024-08-14 15:13:48

如果您不小心添加了析构函数原型,您将收到相同的错误消息。添加空的析构函数定义或删除原型。

You will get the same error message if you accidentally add a destructor prototype. Add an empty destructor definition or remove the prototype.

如此安好 2024-08-14 15:13:48

moc 编译的头文件应包含在 HEADERS += ... 变量中:

我已将 Myproject.pro 中的头文件移至 SOURCES += ... 部分,因为我想要 mySource.h 和 mySource.cpp在同一个树元素中。但这对于 QT Creator 来说是错误的。结果出现错误“未定义对 vtable 的引用”。
似乎是:
QT 仅在 HEADERS +=... 部分(或变量)中检测 moc 编译的标头。
另请参阅其他 stackoverflow 答案中的正确解释第二个答案“我已经看到了很多解决问题的方法,但没有解释为什么会发生,所以这里是。”。在我看来,这是对问题的准确解释,它帮助我发现并解决了我的问题。

Header files for moc compilation should contained in the HEADERS += ... variable:

I have moved the header files in the Myproject.pro to the SOURCES += ... section, because I want to have mySource.h and mySource.cpp in the same tree element. But that is faulty for QT Creator. In result the error "Undefined reference to vtable" has occured.
It seems to be:
QT detects header for moc compilation only in the HEADERS +=... section (or variable).
See also the correct explaination in the other stackoverflow answer Second anwer "I've seen a lot of ways to solve the problem, but no explanation for why it happens, so here goes.". In my mind this is an exactly explaination of the problem, which has help me to found and solve my problem.

落花随流水 2024-08-14 15:13:48

CMake

使用 CMake 时,有趣的是,如果我的 .cpp 和 .h 文件不在同一文件夹中,则 moc、bu 默认无法生成元文件:)

CMake

when using CMake, interestingly enough if my .cpp and .h files are not in the same folder the moc, bu default, fails to generate the meta file :)

北风几吹夏 2024-08-14 15:13:48

使用 Q_OBJECT 但不生成 moc 文件时出现的错误最多
moc 文件定义
类名::metaObject
静态元对象
qt_metacall

这是一个例子:
当源或头使用 Q_OBJECT

/opt/qt/bin/moc ./window.h -o ./moc_window.cpp

Makefile 在 OBJS 中添加 moc_window.o
例如: OBJS=main.o window.o moc_window.o

尝试这个源
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar。 gz

参考http://fatalfeel.blogspot.com /2013/11/qt5-c-building.html

the most errors from using Q_OBJECT but not generate moc file
the moc file define
classname::metaObject
staticMetaObject
qt_metacall

Here is a example:
when source or header using Q_OBJECT

/opt/qt/bin/moc ./window.h -o ./moc_window.cpp

Makefile add moc_window.o in OBJS
Ex: OBJS=main.o window.o moc_window.o

try this source
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar.gz

refer to http://fatalfeel.blogspot.com/2013/11/qt5-c-building.html

慢慢从新开始 2024-08-14 15:13:48

我使用 Qt Creator 来编译和运行我的程序,我不经常使用 Qt 命令提示符。为了消除恼人的错误“vtable Something Something”,我所做的一件事是将以下几行添加到 .pro 文件中。

模板=应用程序

QT += 核心

I am using Qt creator to compile and run my programs, I don't use Qt command prompt often. One thing I did to get rid of the annoying error "vtable something something" is by adding the following lines to .pro file.

TEMPLATE = app

QT += core

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文