2017-02-28 212 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