我正在使用Qt C ++实现图书馆管理系统。我有一个QMainwindow的Material类,当我单击“小说”时,菜单栏中的“小说”部分应打开,这是一个“ QDialogbox”。但是,尽管我实现了这个概念,但是我得到了“在'{'之前的预期类名”的错误。请帮助查找错误。先感谢您。

这是material.h

#ifndef MATERIALS_H
#define MATERIALS_H

#include <QMainWindow>
#include "materialinner.h"
#include "fictionsection.h"
namespace Ui {
class Materials;
}

class Materials : public QMainWindow, public MaterialInner
{
    Q_OBJECT

public:
    explicit Materials(QWidget *parent = 0);
    ~Materials();

private slots:
    void on_btnAdd_clicked();

    void on_btnLoad_clicked();

    void on_btnEdit_clicked();
    void on_tblMaterial_clicked(const QModelIndex &index);
    void on_btnSearch_clicked();

    void on_actionClear_triggered();
    void createAction();

    void on_actionEdit_triggered();

    void on_actionDelete_Records_triggered();

    void on_actionFiction_section_triggered();

private:
    Ui::Materials *ui;
    FictionSection *fic;
};

#endif // MATERIALS_H


这是material.cpp

#include "materials.h"
#include "ui_materials.h"

#include <QDebug>
#include <QMessageBox>

Materials::Materials(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Materials)
{
    ui->setupUi(this);
   // QObject ::connect(ui->lneditSearch,SIGNAL(textChanged(const QString &)),this,SLOT(displaySearch()));
}

Materials::~Materials()
{
    delete ui;
}
void Materials::on_actionFiction_section_triggered()
{
  /*  this->hide();
     fiction = new FictionSection();
    fiction->show();*/
    this->hide();
    fic = new FictionSection();
    fic->show();

}


这是fictionsection.h

#ifndef FICTIONSECTION_H
#define FICTIONSECTION_H

#include <QDialog>
#include "materials.h"
#include "materialinner.h"

namespace Ui {
class FictionSection;
}

class FictionSection : public QDialog, public Materials
**{**
    Q_OBJECT

public:
    explicit FictionSection(QWidget *parent = 0);
    ~FictionSection();

private:
    Ui::FictionSection *ui;

};

#endif // FICTIONSECTION_H


在函数section.cpp类中发生错误。发生错误的花括号为粗体。

使用下面的代码片段,它给出了“请求成员'show'不明确”的错误
Material.cpp

#include "materials.h"
#include "ui_materials.h"
#include "fictionsection.h"

#include <QDebug>
#include <QMessageBox>

Materials::Materials(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Materials)
{
    ui->setupUi(this);
   // QObject ::connect(ui->lneditSearch,SIGNAL(textChanged(const QString &)),this,SLOT(displaySearch()));
}
void Materials::on_actionFiction_section_triggered()
{
    this->hide();
     fiction = new FictionSection();
    fiction->show();
}


如何解决呢?

最佳答案

您具有循环依赖项:materials.h包括fictionsection.h,而fictionsection.h包括materials.h

因为您的头文件具有防止多个包含的例程(#ifndef FICTIONSECTION_H#ifndef MATERIALS_H很好),所以当material.h包含fictionsection.h时,该文件又包含了material.h,但是由于您的多个文件绝对没有作用包含保护....结果是,fictionsection.h最终没有得到Materials声明,并且拒绝声明源自它的FictionSection

您需要使用前向声明来解决该问题:

materials.h中,替换为:

#include "fictionsection.h"


通过

class FictionSection;


并仅在#include "fictionsection.h"中添加materials.cpp

前向声明是解决此问题的常用方法。但是,即使没有发生此问题,前向声明仍然是一个好习惯,因为它将加快编译速度。

关于c++ - 如何在QT C++中的'{'之前解决预期的类名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34102953/

10-13 04:25