This question already has answers here:
Resolve build errors due to circular dependency amongst classes
                                
                                    (11个答案)
                                
                        
                                2年前关闭。
            
                    
我有2个Qt窗口,分别是MainWindowGraphWindow
GraphWindow类具有一系列QCustomPlot小部件,在这些小部件中将绘制来自其他类的数据。在MainWindow实现文件中,我实例化了GraphWindow对象并将其地址传递给其他类,这样所有图都将在同一窗口上生成。但是我的问题是,当我尝试传递GraphWindow对象的地址时,Qt会引发以下错误。


  Unknown type name GraphWindow


我在所有使用graphwindow.h对象的类中都包含了GraphWindow

这是GraphWindow类声明;

#ifndef GRAPHWINDOW_H
#define GRAPHWINDOW_H

#include <QDialog>
#include <mainwindow.h>
#include <incomestatement.h>
#include <balancesheet.h>
#include <cashflow.h>
#include <dcf.h>
#include <QList>
#include <QStringList>
#include <QString>
#include <qcustomplot.h>
#include <QSharedPointer>
#include <QHash>

namespace Ui
{
    class GraphWindow;
}



class GraphWindow : public QDialog
{
    Q_OBJECT

public:
    explicit GraphWindow(QWidget *parent = 0);
    ~GraphWindow();
    void plotData(QStringList labels, QList<double> yData,QString xLabel, QString yLabel, QString slot);

private:
    Ui::GraphWindow *ui;
    QHash<QString, QCustomPlot* > dynamicWidgetHash;
    QStringList widgetStringList;

    QVector<double> setupXAxis(QStringList labels, QString slot, QString xLabel);
    void setupYAxis(QVector<double> yData, QString slot, QString yLabel);
    void setupLegend(QString slot);
    void setHash(QStringList widgetStringList);
};

#endif // GRAPHWINDOW_H


这是创建GrapWindow对象的mainwindow.cpp

void MainWindow::on_runButton_clicked()
{
    //Create grapher object and pass the address to all sub-classes to plot on the same widget
    GraphWindow graphWindow;

    if(globalPath.isEmpty() == false)
    {

        //Create input QXlsx::Document Object
        QXlsx::Document inputDoc(globalPath);

//      QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
//                                   "/home/jana/untitled.png",
//                                   tr("Images (*.png *.xpm *.jpg)"));

        //Create financial statement objects and compute values
        IncomeStatement incState(inputDoc,graphWindow);
        incState.computePoints(MainWindow::pointsListINC_ST,MainWindow::incSettingList,
                               MainWindow::incItemList,inputDoc);
    }
}


这是incomestatement.h,如果类使用GraphWindow对象则为1。

#ifndef INCOMESTATEMENT_H
#define INCOMESTATEMENT_H

#include <QString>
#include <xlsxdocument.h>
#include <xlsxformat.h>
#include <QVector>
#include <QList>
#include <pointssystem.h>
#include <graphwindow.h>

class IncomeStatement
{
    //Declare PointsSystem as FRIEND of IncomeStatement to access private members
    friend class PointsSystem;
public:
    IncomeStatement(QXlsx::Document& inputDoc, GraphWindow& grapher);

    //Public member function to be called from mainwindow.cpp
    double computePoints(QList<QList<bool> > userSettings, QList<QStringList> itemStringList,
                         QStringList comboBoxItems, QXlsx::Document& inputDoc);

private:
    int cursorRow;
    int cursorCol;
    double scoredPoints;
    double totalPoints;

    QStringList timelineList;
    QList<double> revenueGrowthArr;
    QList<double>costOfRevenGrowthArr;
    QList<double>operIncGrowthArr;
    QList<double>netIncGrowthArr;
    QList<double>totOperExpGrowthArr;
    QList<double>grossMarginArr;
    QList<double>opIncMarginArr;
    QList<double>netIncMarginArr;


    QXlsx::Format format(QXlsx::Document& inputDoc, int mode);
    bool setSheetName(QXlsx::Document& inputDoc);
    void process(QXlsx::Document& inputDoc, GraphWindow& grapher);
    int searchKey(QString key,QXlsx::Document& inputDoc);
    int findCursorPointRow(QXlsx::Document& inputDoc);
    int findCursorPointCol(QXlsx::Document& inputDoc);
    void revenueGrowth(QXlsx::Document& inputDoc);
    void growthComputation(QXlsx::Document& inputDoc, QString criterion, QList<double>&storageList);
    void marginComputation(QXlsx::Document& inputDoc, QString criterion, QList<double>&storageList);
    QStringList extractTimeline(QXlsx::Document& inputDoc);
    void writeStockAnalysis(QXlsx::Document& inputDoc, QList<QList<QVariant> > data);
    void writeStockAnalysisHeader(QXlsx::Document& inputDoc);
    void writePointsSumData(QXlsx::Document& inputDoc);

#endif // INCOMESTATEMENT_H


怎么了

最佳答案

您不应该#include <mainwindow.h>中的graphwindow.h,如果mainwindow应该使用graphwindow则没有意义。

如果需要包含,则说明您的设计有问题。在这种情况下,您可以通过在mainwindow.h中包括graphwindow.cpp并为graphwindow.h中的mainwindow提供所需的所有内容来向前声明来规避该问题。
无论如何,您可能需要重新考虑设计,而不是规避问题...

09-05 07:16