使用getter函数时,c ++无法检索正确的值

问题描述 投票:-1回答:1

我试图从“void readJson();”中检索值使用getter的函数。

void readJson() - 我创建的一个函数,用于为我的类中声明的各个变量设置值

我试图将每个变量设置为公共变量但它似乎也不起作用。

    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 
*/

有线索吗?一些帮助将不胜感激(:

c++ class setter getter qtchart
1个回答
0
投票

您的火车线变量(如nsline nsline)在readJsn函数内声明,因此它们超出范围并在函数结束时消失。 您在MainGUI中声明的具有相同名称的重复项未使用且具有随机内容。

你需要在外面声明它们(在MainGUI中,就像你所做的那样),然后在readJsn函数中传递它们以填充它们。只是拥有相同名称的变量不会以任何方式关联它们。

© www.soinside.com 2019 - 2024. All rights reserved.