无效使用不完整类型/前向声明

时间:2013-02-20 15:09:32

标签: c++ qt5

我一直在寻找有关此问题的信息..但我找到的解决方案都没有帮助过我,我真的希望你能帮助我

  • maintabholder.h:

    #ifndef MAINTABHOLDER_H
    #define MAINTABHOLDER_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainTabHolder;
    }
    
    class MainTabHolder : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainTabHolder(QWidget *parent = 0);
        ~MainTabHolder();
    
    private:
        Ui::MainTabHolder *ui;
    };
    #endif // MAINTABHOLDER_H
    
  • maintabholder.cpp:

    #include "maintabholder.h"
    #include "ui_maintabholder.h"
    
    MainTabHolder::MainTabHolder(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainTabHolder)
    {
        ui->setupUi(this);
    }
    
    MainTabHolder::~MainTabHolder()
    {
        delete ui;
    }
    
  • main.cpp中:

    #include "maintabholder.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainTabHolder w;
        w.show();
        return a.exec();
    }
    
  • ui_maintabholder.h             #ifndef UI_MAINTABHOLDER_H     #define UI_MAINTABHOLDER_H

    #include <QtCore/QVariant>
    #include <QtWebKitWidgets/QWebView>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QHBoxLayout>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QPushButton>
    #include <QtWidgets/QSpacerItem>
    #include <QtWidgets/QTabWidget>
    #include <QtWidgets/QVBoxLayout>
    #include <QtWidgets/QWidget>
    
    QT_BEGIN_NAMESPACE
    
    class Ui_Form
    {
    public:
        QHBoxLayout *horizontalLayout;
        QTabWidget *tabWidget;
        QWidget *comicViewer;
        QWidget *comicSearcher;
        QVBoxLayout *verticalLayout;
        QHBoxLayout *horizontalLayout_2;
        QPushButton *pushButton;
        QSpacerItem *horizontalSpacer;
        QPushButton *pushButton_2;
        QWebView *webView;
    
        void setupUi(QWidget *Form)
        {
            if (Form->objectName().isEmpty())
                Form->setObjectName(QStringLiteral("Form"));
            Form->resize(531, 308);
            horizontalLayout = new QHBoxLayout(Form);
            horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
            tabWidget = new QTabWidget(Form);
            tabWidget->setObjectName(QStringLiteral("tabWidget"));
                    comicViewer = new QWidget();
            comicViewer->setObjectName(QStringLiteral("comicViewer"));
            comicViewer->setCursor(QCursor(Qt::ArrowCursor));
            tabWidget->addTab(comicViewer, QString());
            comicSearcher = new QWidget();
            comicSearcher->setObjectName(QStringLiteral("comicSearcher"));
            verticalLayout = new QVBoxLayout(comicSearcher);
            verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
            horizontalLayout_2 = new QHBoxLayout();
            horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
            pushButton = new QPushButton(comicSearcher);
            pushButton->setObjectName(QStringLiteral("pushButton"));
    
            horizontalLayout_2->addWidget(pushButton);
    
            horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    
            horizontalLayout_2->addItem(horizontalSpacer);
    
            pushButton_2 = new QPushButton(comicSearcher);
            pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
            pushButton_2->setEnabled(false);
    
            horizontalLayout_2->addWidget(pushButton_2);
    
    
            verticalLayout->addLayout(horizontalLayout_2);
    
            webView = new QWebView(comicSearcher);
            webView->setObjectName(QStringLiteral("webView"));
            webView->setUrl(QUrl(QStringLiteral("about:blank")));
    
            verticalLayout->addWidget(webView);
    
            tabWidget->addTab(comicSearcher, QString());
    
            horizontalLayout->addWidget(tabWidget);
    
    
            retranslateUi(Form);
    
            tabWidget->setCurrentIndex(1);
    
    
            QMetaObject::connectSlotsByName(Form);
        } // setupUi
    
        void retranslateUi(QWidget *Form)
        {
            Form->setWindowTitle(QApplication::translate("Form", "Form", 0));
            tabWidget->setTabText(tabWidget->indexOf(comicViewer), QApplication::translate("Form", "Comic Viewer", 0));
            pushButton->setText(QApplication::translate("Form", "Go to SubManga", 0));
            pushButton_2->setText(QApplication::translate("Form", "Download Comic", 0));
            tabWidget->setTabText(tabWidget->indexOf(comicSearcher), QApplication::translate("Form", "Search For Comics", 0));
        } // retranslateUi
    
    };
    
    namespace Ui {
        class Form: public Ui_Form {};
    } // namespace Ui
    
    QT_END_NAMESPACE
    
    #endif // UI_MAINTABHOLDER_H
    

编译器错误是:

    [...]\maintabholder.cpp: In constructor 'MainTabHolder::MainTabHolder(QWidget*)':
    [...]\maintabholder.cpp:6:16: error: invalid use of incomplete type 'class Ui::MainTabHolder'
    In file included from [...]\maintabholder.cpp:1:0:
    [...]\maintabholder.h:7:7: error: forward declaration of 'class Ui::MainTabHolder'
    [...]\maintabholder.cpp:8:7: error: invalid use of incomplete type 'class Ui::MainTabHolder'
    In file included from [...]\maintabholder.cpp:1:0:
    [...]\maintabholder.h:7:7: error: forward declaration of 'class Ui::MainTabHolder'
    [...]\maintabholder.cpp: In destructor 'virtual MainTabHolder::~MainTabHolder()':
    [...]\maintabholder.cpp:13:12: warning: possible problem detected in invocation of delete operator: [enabled by default]
    [...]\maintabholder.cpp:13:12: warning: invalid use of incomplete type 'class Ui::MainTabHolder' [enabled by default]
    In file included from [...]\maintabholder.cpp:1:0:
    [...]\maintabholder.h:7:7: warning: forward declaration of 'class Ui::MainTabHolder' [enabled by default]
    [...]\maintabholder.cpp:13:12: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined

你能帮忙吗?我很感激...

2 个答案:

答案 0 :(得分:3)

您正在使用Qt的uic生成的文件;您应该验证此工具是否成功生成文件,例如检查构建输出和文件本身。另外,请检查它是否在*.pro文件中(即maintabholder.h标记为由uic处理。)

我个人建议将multiple inheritance method用于ui:

// in maintabholder.h
// ...
#include "ui_maintabholder.h"

class MainTabHolder : public QMainWindow, public Ui::MainTabHolder
{
    // ...
};

使用起来更简单,如果您对ui_*.h文件有疑问,则错误更明确。

您应该发布ui_maintabholder.h

答案 1 :(得分:0)

您在.cpp文件中包含了ui define head,并定义了一个私有成员“Ui :: MainTabHolder * ui;”在你的.h文件中,所以你需要在你的课程定义之前在.h文件(maintabholder.h :)中添加一个前向声明:

class Ui_MainTabHolder;

这里Ui_MainTabHolder是您的Ui类名称,请检查您自己的项目是否正确。