簡體   English   中英

c++ 使用 getter 函數時無法檢索正確的值

[英]c++ unable to retrieve the correct values when using the getter function

我正在嘗試從“void readJson();”中檢索值使用 getter 函數。

void readJson() - 我創建的一個函數,用於為我的類中聲明的各個變量設置值

我試圖將每個變量設置為 public,但它似乎也不起作用。

    void readJson()
    {

        ...

        QJsonArray json_array = json_doc.array();

        /*Instantiate QJsonArray class object*/

        TrainServiceDisruptionData tsds;
        for (int i = 0; i < json_array.count(); ++i) {
            tsds.setLine(json_array.at(i).toObject().value("line"));

        }

        //Train Lines
        nsline nsline;
        ewline ewline;
        neline neline;
        ccline ccline;
        dtline dtline;
        orline orline;

        //Initializing values
        int nsl = 0, ewl = 0, nel = 0, ccl = 0, dtl = 0, orl = 0;
        int totalline = tsds.getLine().size();
        for (int i = 0; i < totalline; ++i) {
            if (tsds.getLine().at(i) == "North-South Line")
            {
                nsl += 1;
            }
            else if (tsds.getLine().at(i) == "East-West Line")
            {
                ewl += 1;
            }
            else if (tsds.getLine().at(i) == "North-East Line")
            {
                nel += 1;
            }
            else if (tsds.getLine().at(i) == "Circle Line")
            {
                ccl += 1;
            }
            else if (tsds.getLine().at(i) == "Downtown Line")
            {
                dtl += 1;
            }
            else
            {
                orl += 1;
            }
        }

        nsline.setlines(nsl);
        ewline.setlines(ewl);
        neline.setlines(nel);
        ccline.setlines(ccl);
        dtline.setlines(dtl);
        orline.setlines(orl);

        qDebug() << "<Respective Train Breakdown Count>" << endl
            << "North-South Line: " << nsline.getlines() << endl
            << "East-West Line: " << ewline.getlines() << endl
            << "North-East Line: " << neline.getlines() << endl
            << "Circle Line: " << ccline.getlines() << endl
            << "Downtown Line: " << dtline.getlines() << endl
            << "Other Lines: " << orline.getlines() << endl
            << "Total Count: " << totalline << endl;

         }
         else
         {
            qDebug() << "file does not exists" << endl;
            exit(1);
            file.close();
         }
    }

... The codes below are where I use getter function to retrieve those values from setter function declared in readJson function...

    void MainGUI::showpiechartbtnReleased()
    {
        nsline nsline;
        ewline ewline;
        neline neline;
        ccline ccline;
        dtline dtline;
        orline orline;

        int nsl = nsline.getlines();
        int ewl = ewline.getlines();
        int nel = neline.getlines();
        int ccl = ccline.getlines();
        int dtl = dtline.getlines();
        int orl = orline.getlines();

        //Plot pie chart data
        QPieSeries *series = new QPieSeries();
        series->append("North-South Line", nsl);
        series->append("East-West Line", ewl);
        series->append("North-East Line", nel);
        series->append("Circle Line", ccl);
        series->append("Downtown Line", dtl);
        //series->append("Other Lines", orl); 
        //no idea why qt pie chart unable to display 6 data

        qDebug() << "<Respective Train Breakdown Count 2>" << endl
        << "North-South Line: " << nsline.getlines() << endl
        << "East-West Line: " << ewline.getlines() << endl
        << "North-East Line: " << neline.getlines() << endl
        << "Circle Line: " << ccline.getlines() << endl
        << "Downtown Line: " << dtline.getlines() << endl
        << "Other Lines: " << orline.getlines() << endl;

        QPieSlice *slice = series->slices().at(1);
        slice->setExploded();
        slice->setLabelVisible();
        slice->setPen(QPen(Qt::darkGreen, 2));
        slice->setBrush(Qt::green);

        QChart *chart = new QChart();
        chart->addSeries(series);
        chart->setTitle("MRT Disruption Pie Chart");
        //chart->legend()->hide();

        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);

        //Display chart 
        ui.verticalLayout_11->addWidget(chartView);

        //Return back to line colour chart display page 
        ui.stackedWidget->setCurrentIndex(5);

        //Remove previous chart to prevent duplicate
        ui.verticalLayout_11->removeWidget(chartView);
    }

調試后,我應該得到這些類似的值作為我的輸出:

/*
><Respective Train Breakdown Count> 
>North-South Line:  190 
>East-West Line:  203 
>North-East Line:  50 
>Circle Line:  66 
>Downtown Line:  12 
>Other Lines:  53 
>Total Count:  574 
*/

但是,我得到了這些:

/*
><Respective Train Breakdown Count 2> 
>North-South Line:  2 
>East-West Line:  1514685496 
>North-East Line:  1953534480 
>Circle Line:  1514685496 
>Downtown Line:  1953534480 
>Other Lines:  -1479339952 
*/

有什么線索嗎? 一些幫助將不勝感激(:

您的火車線路變量(如nsline nsline )在readJsn函數聲明,因此它們超出范圍並在函數結束時消失。
您在MainGUI中聲明的MainGUI不會被使用並且具有隨機內容。

您需要在外部聲明它們(在MainGUI ,正如您所做的那樣),並將它們傳遞到readJsn函數中以進行填充。 僅具有相同名稱的變量不會以任何方式將它們關聯起來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM