c++ - 不完整类型 Qt 的无效使用

标签 c++ qt linker base-class

我有一个小问题:

finddialog.h

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

class QDialog;
class QWidget;
class QLineEdit;
class QPushButton;
class QCheckBox;
class QLabel;

class FindDialog : public QDialog
{
    Q_OBJECT


public:
    FindDialog(QWidget *parent);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

signals :
    void findNext(const QString &str, Qt::CaseSensitivity cs);
    void findPrev(const QString &str, Qt::CaseSensitivity cs);

private:
    QLabel *findLabel;
    QLineEdit *textEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;

};

#endif // FINDDIALOG_H

finddialog.c

#include <QtWidgets>
#include "finddialog.h"


FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{
    QPushButton *button = new QPushButton(this);
}

void FindDialog::findClicked()
{

}

void FindDialog::enableFindButton(const QString &text)
{

}

我收到一个 QDialog/QPushButton invalid use of incomplete type 错误。

我已经将 QtWidget 包含在 .cpp 文件中,为什么它不起作用?

最佳答案

(Forward) 声明对于基类类型是不够的。您必须提供定义(使 QDialog 成为完整类型),因此:

#include <QDialog>

在头文件中。参见 Forward Declaration of a Base Class .

根据您发布的代码,我不知道关于 QPushButton 的错误在哪里不完整来自(如果你是 #include <QtWidgets>,当然)。

如果你不#include <QtWidgets> (其中有 #include <QPushButton> 等...),您不能实例化任何这些不完整的类型,如下所示:

new QPushButton(this);

这又需要一个定义(QPushButton 需要的内存大小是多少?要调用什么构造函数?只有在该编译单元 - .cpp 文件中有定义时才知道)。

关于c++ - 不完整类型 Qt 的无效使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34559153/

相关文章:

c++ - QApplication 子对象在系统关闭时不会被销毁

c++ - OSX 10.8.2 上的 Poco C++ 库 : Undefined symbols for architecture x86_64

stream is false 和 eof() 之间的 C++ 区别

c++ - 只创建一个对象,仍然调用了 Destructor 2 次。为什么?

c++ - 访问 QTabWidget 的小部件

c++ - 是否有一个函数可以将我创建的类中的对象显示到 QTextBrowser 中?

c - 删除 DWARF-2 重复符号会耗尽内存

c++ - 列出未使用的符号

c++ - 为方法提供一个空实例是一种好习惯吗?

c++ - Windows x86的PostgreSQL 11安装程序?