【问题标题】:Invalide use of incomplete type Qt不完整类型Qt的无效使用
【发布时间】:2016-04-06 04:14:48
【问题描述】:

我有个小问题:

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 无效使用不完整类型错误。

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

【问题讨论】:

    标签: c++ qt linker base-class


    【解决方案1】:

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

    #include <QDialog>
    

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

    根据您发布的代码,我不知道QPushButton 不完整的错误来自哪里(当然,如果您是#include &lt;QtWidgets&gt;)。

    如果您不使用#include &lt;QtWidgets&gt;(其中有#include &lt;QPushButton&gt; 等...),则无法实例化任何这些不完整类型,如下所示:

    new QPushButton(this);
    

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

    【讨论】:

    • 为了清楚起见,这不是声明,而是前向声明。
    • @skypjack "forward" 只是表示用法,对吗?它仍然是一个声明。
    • @LogicStuff 为了清楚起见
    • @LogicStuff 没错,之所以这样称呼它是因为我们对它的使用。有关详细信息,请参阅here
    • 谢谢,另一个人认为了解了 c++,但我不知道为什么我对 QPushButton 和我想使用的任何其他 Qt 类型有错误,但是当我在头文件中包含 时一切正常
    【解决方案2】:

    通过QPushButtonfinddialog.h 中的其他人的前向声明,编译器希望在某些链接文件中找到它们的构造函数和其他方法。
    #include 所需的头文件使其工作。

    【讨论】:

    • @jafar 在头文件中没有取消引用指针的内联方法,并且在源文件中我包含了
    猜你喜欢
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2017-10-24
    • 2010-10-13
    • 2013-05-19
    • 2017-07-28
    • 1970-01-01
    相关资源
    最近更新 更多