如何从方法的每个部分的变量中获得相同的数据

How can I get the same data from a variable in every part of the method?

本文关键字:数据 变量 方法 个部      更新时间:2023-10-16

我遇到了这样的困境:使用下面的类似代码:

//floatArray[totalKeyPoints] declared just outside this loop
for(int d = 0; d < totalGroups; d++)
    {
        std::string STRING = "";
        QString lk = "test" + QString::number(d) + ".txt";
        std::string kl;
        kl = lk.toStdString();
        std::ifstream infile(kl.c_str());
        //std::cout << "FILEPATH: " << kl.c_str() << std::endl;
        int gl = 0;
        while(endFile != true)
        {
            endFile = infile.eof();
            getline(infile,STRING);
            if(STRING.empty() == false && endFile == false && gl < totalKeyPoints)
            {
                QString temp = STRING.c_str();
                QStringList temp1 = temp.split(" ");
                QString tmp = temp1[0];
                floatArray[gl].response = tmp.toFloat();
                tmp = temp1[1];
                floatArray[gl].angle = tmp.toFloat();
                tmp = temp1[2];
                floatArray[gl].size = tmp.toFloat();
                tmp = temp1[3];
                floatArray[gl].class_id = tmp.toInt();
                if(firstRun2 == true)
                    qDebug() << "A:"
                             << floatArray[gl].response 
                             << floatArray[gl].angle 
                             << floatArray[gl].size 
                             << floatArray[gl].class_id; //TAKE NOTE
                int xs = tmp.toInt();
                kpPerGroup[xs]++;
                gl++;
            }
        }
        infile.close();
        if(d < totalGroups)
        {
            endFile = false;
        }
    }
    for (int d = 0; d < totalKeyPoints; d++)
    {
        if(firstRun2 == true)
            qDebug() << "B:" 
                     << floatArray[d].response 
                     << floatArray[d].angle 
                     << floatArray[d].size << floatArray[d].class_id; //TAKE NOTE
    }

我已经在这个粘贴框中记录了两个qDebug命令的输出:使用A和B

为什么我会有这种行为?有没有一种方法可以让我在整个代码中只有SET a值?

我猜您希望您的"A"调试输出实际为:

 qDebug() << "A:" << floatArray[gl].response << floatArray[gl].angle <<  
                     floatArray[gl].size << floatArray[gl].class_id;

对所有索引使用CCD_ 2而不是对最后三个使用d。在第一个循环中,索引CCD_ 4似乎与CCD_ 5无关。往好了说会得到不正确的输出,往坏了说会出现数组溢出/崩溃。