断言QT Creator中的失败

时间:2016-02-19 02:52:50

标签: c++ qt csv indexing

我有一个程序,它运行CSV文件,并将它们编译成两个文本文件,一个包含漂亮行中的所有CSV文件,另一个提供快速摘要也对它们进行排名。把所有东西放在一起后,我就跑了。程序输出"做QString"后,我收到此消息:

  

在QList :: operator []中的ASSERT失败:&#34;索引超出范围&#34;,文件........ \ 5.3 \ mingw482_32 \ include / QtCore / qlist.h,第487行< / p>

我的程序副本:

    #include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QDebug>
#include <QString>
#include <QTextStream>

// Variables for the score names

#define Team                0
#define Match               1
#define Pos    2
#define AutoDefCross           3
#define AutoCrossed            4
#define HighAttempt             5
#define HighMade               6
#define LowMade              7
#define LowAttempt           8
#define TeleLow               9
#define TeleHigh           10
#define BarClear            11
#define DrawAss            12
#define DrawClear           13
#define DrawFail          14
#define MoatClear         15
#define MoatFail           16
#define PortAss              17
#define PortClear            18
#define PortFail             19
#define RamClear          20
#define RamFail             21
#define SwingAss          22
#define SwingClear            23
#define SwingFail          24
#define TeetAss           25
#define TeetClear          26
#define TeetFail         27
#define TerraClear       28
#define TerraFail       29
#define Wallclear      30
#define WallFail       31
#define Challenge      32
#define Climb         33
#define Comment        34
// Writing AllData

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

        int counter=0, i, j, team;
        int teams[120], matchcount[120], autosum[120], telesum[120], totsum[120];
        int highteamauto, highteamtele, highteamtotal;
        QDir dir("C:/FRCScoutData/TabletFiles");

        QFile myoutfile("C:/FRCScoutData/alldata.csv");

        myoutfile.open(QIODevice::WriteOnly);

        QTextStream outfile(&myoutfile);

        QStringList rawData;
        QStringList mylist;
// The titles of blocks in Excel, respective to lower outfile.
outfile << "Team,Match,totalscore,autoscore,telescore,Pos,AutoDefCross,AutoCross,HighAttempt,HighMade,LowMade,LowAttempt,TeleLow,TeleHigh,BarClear,DrawAss,DrawClear,DrawFail,MoatFail,PortAss,PortClear,PortFail,RamClear,RamFail,SwingAss,SwingClear,SwingFail,TeetAss,TeetClear,TeetFail,TerraClear,TerraFail,Clear,Challenge,Climb,ChevaFail,WallFail,Comment \r\n";


        if ( dir.exists() )
        {
            //set fileinfo filter
            QFileInfoList entries = dir.entryInfoList( QDir::NoDotAndDotDot | QDir::Files );
            //loop over entries filter selected
            foreach ( QFileInfo entryInfo, entries )
            {
                QString path = entryInfo.absoluteFilePath();

                QFile csvfile(path);

                printf("Processing file %s\n", qPrintable(path));

                csvfile.open(QIODevice::ReadOnly);

                QTextStream stream(&csvfile);

                rawData.append(stream.readAll());

                mylist = rawData[counter].split(",");
                counter++;
// Defining scores, or something like that. ||||||||iI|||

                printf("Doing QString");
                QString autoscore = QString::number((mylist[AutoDefCross].toInt())           *   1+
                                                                    (mylist[AutoCrossed].toInt())            *   1+
                                                                    (mylist[HighAttempt].toInt())             *   1+
                                                                    (mylist[HighMade].toInt())         *   2+
                                                                    (mylist[LowMade].toInt())       *   2+
                                                                    (mylist[LowAttempt].toInt())         *   1);

                                QString telescore = QString::number((mylist[TeleLow].toInt())         *   1+
                                                                    (mylist[TeleHigh].toInt())          *   2+
                                                                    (mylist[BarClear].toInt())          *   1+
                                                                    (mylist[DrawAss].toInt())           *   1+
                                                                   (mylist[DrawClear].toInt())      *   1+
                                                                    (mylist[DrawFail].toInt())    *   -1+
                                                                    (mylist[MoatClear].toInt())           *   1+
                                                                    (mylist[MoatFail].toInt())   *   -1+
                                                                    (mylist[PortAss].toInt())          *   1+
                                                                    (mylist[PortClear].toInt())           *   1+
                                                                    (mylist[PortFail].toInt())       *   -1+
                                                                    (mylist[RamClear].toInt())          *  1+
                                                                    (mylist[RamFail].toInt())        *  -1+
                                                                    (mylist[SwingClear].toInt())            *  1+
                                                                    (mylist[SwingFail].toInt())        *  -1+
                                                                    (mylist[TeetAss].toInt())    *   1+
                                                                    (mylist[TeetClear].toInt())           *   1+
                                                                    (mylist[TeetFail].toInt())   *      -1+
                                                                    (mylist[TerraClear].toInt())          *   1+
                                                                    (mylist[TerraFail].toInt())           *  -1+
                                                                    (mylist[Wallclear].toInt())       *   2+
                                                                    (mylist[WallFail].toInt())          *  -1+
                                                                    (mylist[Challenge].toInt())        *  1+
                                                                    (mylist[Climb].toInt())            *    3);


                                QString totalscore = QString::number((autoscore.toInt()) +
                                                                    (telescore.toInt()));
//Actual Variables being written, AKA, Lower Outfile

                                outfile << mylist[Team] + "," + mylist[Match] + "," + totalscore + "," + autoscore + "," + telescore + "," +
                                                           mylist[Pos] + "," + mylist[AutoDefCross] + "," + mylist[AutoCrossed] + "," +
                                                           mylist[HighAttempt] + "," + mylist[HighMade] + "," + mylist[LowMade] + "," + mylist[LowAttempt] + "," +
                                                           mylist[TeleLow] + "," + mylist[TeleHigh] + "," + mylist[BarClear] + "," +
                                                           mylist[DrawAss] + "," + mylist[DrawClear] + "," + mylist[DrawFail] + "," + mylist[MoatClear] + "," +
                                                           mylist[PortAss] + "," + mylist[PortClear] + "," + mylist[PortFail] + "," +
                                                           mylist[RamClear] + "," + mylist[RamFail] + "," + mylist[SwingAss] + "," +
                                                           mylist[SwingClear] + "," + mylist[SwingFail] + "," + mylist[TeetAss]
                                                           + "," + mylist[TeetClear] + "," + mylist[TeetFail] + "," + mylist[TerraClear] + "," + mylist[TerraFail] + "," + mylist[Wallclear] +  "," + mylist[WallFail] + "," + "," + mylist[Challenge] + "," + mylist[Climb] + "," + mylist[Comment].simplified() + "\"\r\n";
            }
        }
        else
        {
            // What happens when it fails, I think.
            qDebug() << "Missing directory C:\\FRCScoutData\\TabletFiles, You done messed up, Bro.";
        }



        myoutfile.close();

        rawData.sort();

        int lastteam = 0;
        int teamcount = 0;

        QFile mysummaryfile("C:/FRCScoutData/summarydata.csv");

        mysummaryfile.open(QIODevice::WriteOnly);

        QTextStream outsumfile(&mysummaryfile);
// Begin alldata
// Excel table columns, respective to lower outfile.
        outsumfile << "Team,Match,totalscore,autoscore,telescore,Pos,AutoDefCross,AutoCross,HighAttempt,HighMade,LowMade,LowAttempt,TeleLow,TeleHigh,BarClear,DrawAss,DrawClear,DrawFail,MoatFail,PortAss,PortClear,PortFail,RamClear,RamFail,SwingAss,SwingClear,SwingFail,TeetAss,TeetClear,TeetFail,TerraClear,TerraFail,Clear,Challenge,Climb,ChevaFail,WallFail,Comment \r\n";
        for (i = 0; i < counter; i++)
        {
            mylist = rawData[i].split(",");

            if (lastteam != mylist[Team].toInt())
            {
                if (lastteam != 0)
                {
                    teamcount++;
                }
                teams[teamcount] = mylist[Team].toInt();
                autosum[teamcount] = 0;
                telesum[teamcount] = 0;
                totsum[teamcount] = 0;
                matchcount[teamcount] = 0;
            }
// More score definitions

            lastteam = mylist[Team].toInt();
                        matchcount[teamcount]++;

                        autosum[teamcount] +=  mylist[AutoDefCross].toInt()           *   1+
                                               mylist[AutoCrossed].toInt()            *   1+
                                               mylist[HighAttempt].toInt()             *   1+
                                               mylist[HighMade].toInt()         *   2+
                                               mylist[LowMade].toInt()       *   2+
                                               mylist[LowAttempt].toInt()         *   1;

                        telesum[teamcount] += mylist[TeleLow].toInt()         *   1+
                                              mylist[TeleHigh].toInt()          *   2+
                                              mylist[BarClear].toInt()          *   1+
                                              mylist[DrawAss].toInt()           *   1+
                                              mylist[DrawFail].toInt()      *   1+
                                              mylist[MoatClear].toInt()    *   1+
                                              mylist[MoatFail].toInt()           *   -1+
                                              mylist[PortAss].toInt()   *   1+
                                              mylist[PortClear].toInt()          *   1+
                                              mylist[PortFail].toInt()           *   -1+
                                              mylist[RamClear].toInt()       *   1+
                                              mylist[RamFail].toInt()          *  -1+
                                              mylist[SwingAss].toInt()        *  1+
                                              mylist[SwingFail].toInt()            *  -1+
                                              mylist[TeetAss].toInt()        *   1+
                                             (mylist[TeetClear].toInt())           *   1+
                                              (mylist[TeetFail].toInt())   *      -1+
                                             (mylist[TerraClear].toInt())          *   1+
                                           (mylist[TerraFail].toInt())           *  -1+
                                           (mylist[Wallclear].toInt())       *   2+
                                           (mylist[WallFail].toInt())          *  -1+
                                           (mylist[Challenge].toInt())        *  1+
                                            (mylist[Climb].toInt())            *    3;


                        totsum[teamcount] +=  mylist[AutoDefCross].toInt()           *   1+
                                mylist[AutoCrossed].toInt()            *   1+
                                mylist[HighAttempt].toInt()             *   1+
                                mylist[HighMade].toInt()         *   2+
                                mylist[LowMade].toInt()       *   2+
                                mylist[LowAttempt].toInt()         *   1+

                                (mylist[TeleLow].toInt())         *   1+
                                (mylist[TeleHigh].toInt())          *   2+
                                (mylist[BarClear].toInt())          *   1+
                                (mylist[DrawAss].toInt())           *   1+
                                (mylist[DrawClear].toInt())      *   1+
                                (mylist[DrawFail].toInt())    *   -1+
                                (mylist[MoatClear].toInt())           *   1+
                                (mylist[MoatFail].toInt())   *   -1+
                                (mylist[PortAss].toInt())          *   1+
                                (mylist[PortClear].toInt())           *   1+
                                (mylist[PortFail].toInt())       *   -1+
                                (mylist[RamClear].toInt())          *  1+
                                (mylist[RamFail].toInt())        *  -1+
                                (mylist[SwingClear].toInt())            *  1+
                                (mylist[SwingFail].toInt())        *  -1+
                                (mylist[TeetAss].toInt())    *   1+
                                (mylist[TeetClear].toInt())           *   1+
                                (mylist[TeetFail].toInt())   *      -1+
                                (mylist[TerraClear].toInt())          *   1+
                                (mylist[TerraFail].toInt())           *  -1+
                                (mylist[Wallclear].toInt())       *   2+
                                (mylist[WallFail].toInt())          *  -1+
                                (mylist[Challenge].toInt())        *  1+
                                (mylist[Climb].toInt())            *    3;

        }


        for (team=0; team <= teamcount; team++)
        {
           highteamauto = 1;
           highteamtele = 1;
           highteamtotal = 1;
           for (j = 0; j <= teamcount; j++)
           {
               /*if ((float(autosum[j])/float(matchcount[j])) > (float(autosum[team])/float(matchcount[team])))
               {
                   highteamauto++;
               }
               if ((float(telesum[j])/float(matchcount[j])) > (float(telesum[team])/float(matchcount[team])))
               {
                   highteamtele++;
               }
               if ((float(endsum[j])/float(matchcount[j])) > (float(endsum[team])/float(matchount[team])))
               {
                   highteamend++;
               {
               if ((float(totsum[j])/float(matchcount[j])) > (float(totsum[team])/float(matchcount[team])))
               {
                   highteamtotal = highteamtotal + 1;
               }
               */
               if ((autosum[j]/matchcount[j]) > (autosum[team]/matchcount[team]))
               {
                   highteamauto++;
               }
               if ((telesum[j]/matchcount[j]) > (telesum[team]/matchcount[team]))
               {
                   highteamtele++;
               }
               if ((totsum[j]/matchcount[j]) > (totsum[team]/matchcount[team]))
               {
                   highteamtotal = highteamtotal + 1;
               }
           }

           // Output summary record, AKA, Lower Outfile
                      outsumfile << QString::number(teams[team])      + "," +
                                    QString::number(matchcount[team]) + "," +
                                    mylist[HighMade].toInt()       + "," +
                                    mylist[LowMade].toInt()       + "," +
                                    mylist[TeleHigh].toInt()       + "," +
                                    mylist[TeleLow].toInt()       + "," +
                                    mylist[BarClear].toInt()       + "," +
                                    mylist[Climb].toInt()       + "," +
                                    mylist[Comment].toInt()       + "," +
                                    QString::number(autosum[team])    + "," +
                                    QString::number(float(autosum[team])/float(matchcount[team]), 'f', 1) + "," +
                                    QString::number(telesum[team])    + "," +
                                    QString::number(float(telesum[team])/float(matchcount[team]), 'f', 1) + "," +
                                    QString::number(totsum[team])     + "," +
                                    QString::number(float(totsum[team])/float(matchcount[team]), 'f', 1)  + "," +
                                    QString::number(highteamauto)     + "," +
                                    QString::number(highteamtele)     + "," +
                                    QString::number(highteamtotal)    + "\r\n";

                      qDebug() << QString::number(teams[team])      + "," +
                                  QString::number(matchcount[team]) + "," +
                                  mylist[HighMade].toInt()       + "," +
                                  mylist[LowMade].toInt()       + "," +
                                  mylist[TeleHigh].toInt()       + "," +
                                  mylist[TeleLow].toInt()       + "," +
                                  mylist[BarClear].toInt()       + "," +
                                  mylist[Climb].toInt()       + "," +
                                  mylist[Comment].toInt()       + "," +
                                  QString::number(autosum[team])    + "," +
                                  QString::number(float(autosum[team])/float(matchcount[team]), 'f', 1) + "," +
                                  QString::number(telesum[team])    + "," +
                                  QString::number(float(telesum[team])/float(matchcount[team]), 'f', 1) + "," +
                                  QString::number(totsum[team])     + "," +
                                  QString::number(float(totsum[team])/float(matchcount[team]), 'f', 1)  + "," +
                                  QString::number(highteamauto)     + "," +
                                  QString::number(highteamtele)     + "," +
                                  QString::number(highteamtotal)    + "\r\n";

        }

        mysummaryfile.close();
}

请原谅这个烂摊子。

1 个答案:

答案 0 :(得分:1)

检查QFile csvfile中的每一行是否包含正确的条目数。我把钱放在某个地方有一个逗号丢失的地方。该错误表明索引超出范围,QStringList mylist的范围由rawData[counter].split(",");创建的字段数设置。也许你可以在每次迭代时输出程序myList.size(),并确保你获得一致的输出(在这种情况下,我认为应该是35。