Invalide use of incomplete type Qt(不完整类型 Qt 的无效使用)
问题描述
我有个小问题:
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 无效使用不完整类型错误.
I receive a QDialog/QPushButton invalid use of incomplete type error.
我已经在 .cpp 文件中包含了 QtWidget 为什么它不起作用?
I have already included QtWidget in .cpp file so why is it not working?
推荐答案
(Forward) 声明对于基类类型是不够的.你必须提供定义(使 QDialog 成为一个完整的类型),所以:
(Forward) declaration is not enough for a base-class type. You have to provide the definition (make QDialog a complete type), so:
#include <QDialog>
在头文件中.请参阅基类的前向声明.
根据您发布的代码,我不知道 QPushButton 不完整的错误来自哪里(当然,如果您 #include <QtWidgets>).
Based on the code you posted, I don't know where the error about QPushButton being incomplete comes from (if you #include <QtWidgets>, of course).
如果你不 #include <QtWidgets>(其中有 #include <QPushButton> 等...),你不能实例化任何这些不完整的类型,像这样:
If you do not #include <QtWidgets> (which has #include <QPushButton>, etc...), you cannot instantiate any of these incomplete types, like so:
new QPushButton(this);
同样,需要定义(QPushButton 需要的内存大小是多少?调用什么构造函数?只有在该编译单元中有定义时才知道 - .cpp代码>文件).
That again, requires a definition (What's the size of the memory QPushButton needs? What constructor to call? That's known only if there's a definition in that compilation unit - .cpp file).
这篇关于不完整类型 Qt 的无效使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:不完整类型 Qt 的无效使用
				
        
 
            
        基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
 - 如何通过C程序打开命令提示符Cmd 2022-12-09
 - 这个宏可以转换成函数吗? 2022-01-01
 - 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
 - 如何在 C++ 中初始化静态常量成员? 2022-01-01
 - 我有静态或动态 boost 库吗? 2021-01-01
 - 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
 - 如何检查GTK+3.0中的小部件类型? 2022-11-30
 - C++结构和函数声明。为什么它不能编译? 2022-11-07
 - 在 C++ 中计算滚动/移动平均值 2021-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				