2017-02-28 209 views
0

我有2個Qt窗口,即MainWindowGraphWindow; GraphWindow類的小部件範圍爲QCustomPlot,其中將繪製來自其他類的數據。在MainWindow實現文件中,我實例化一個GraphWindow對象並將其地址傳遞給其他類,以便所有的圖將在同一個窗口中生成。但是我的問題是,當我嘗試傳遞對象的地址時,Qt會拋出以下錯誤;未知類型名稱錯誤Qt C++

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 

這是mainwindow.cpp其中創建GrapWindow對象;

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); 
    } 
} 

此如果使用GraphWindow對象的類是incomestatement.h,一個。

#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 

這裏有什麼錯?

+3

你有一個環形集依賴.. – drescherjm

+0

我的天啊,我不能這個愚蠢的:/謝謝老兄:) – Vino

+0

當我開始學習C++ 25到30年前我確信我做了這麼多次。這些天它從來沒有發生在我身上。 – drescherjm

回答

1

你不應該#include <mainwindow.h>graphwindow.h,它是沒有意義的,如果mainwindow應該使用graphwindow。

如果您需要包含,您的設計有問題。在這種情況下,您可以通過從graphwindow.cpp中包含mainwindow.h,並提供graphwindow.h中mainwindow所需的所有內容的前向聲明來避開該問題。 總之,而不是迴避問題,你可能要重新考慮設計...

+0

你是對的,我沒有使用它,我錯誤地將它複製到那裏;前向聲明的含義,在'MainWindow'聲明中聲明'GraphWindow'的對象?我對麼? – Vino

+0

不,不是真的。第一:我的意思是相反 - 即在graphwindow.h中聲明'mainWindow'。第二:你不會聲明一個對象,而是聲明一個對象,比如:class MainWindow;' – St0fF

+1

另一個注意:如果你的項目變得相當大,那麼轉發聲明類而不是包含它們的頭文件可以優化編譯時間。但是,只有當你聲明要使用被包含的類的時候,它才能起作用,並且是引用和指針。 – St0fF