简体   繁体   English

对指针向量进行排序后,一旦工作便崩溃,并显示“无法读取内存”

[英]sorting a vector of pointers works once then crashes with “unable to read memory”

I have a vector of Flight pointers that I pass into a function to be sorted and displayed on the screen. 我有一个向量的Flight pointers ,我将其传递给要排序并显示在屏幕上的函数。 I am using a functor in the Flight class to do the sorting. 我在Flight class使用函子进行排序。 It works perfectly the first time, then after the sortCriteria is incremented, it crashes when it tries to step through the flights vector the second time. 第一次可以完美运行,然后在sortCriteria递增后,第二次尝试遍历飞行向量时会崩溃。 The error I get says Access violation reading location 0x013DFFFC , so I'm betting it's got something to do with loosing track of the memory location of the vector after it gets sorted the first time. 我得到的错误是Access violation reading location 0x013DFFFC ,因此我Access violation reading location 0x013DFFFC ,它与向量在第一次排序后丢失存储位置有关。 Thanks in advance for any help you can offer. 在此先感谢您提供的任何帮助。 Here is my sorting function: 这是我的排序功能:

//this function displays the flight schedule and sorts it by any field
void showFlightSchedule(vector<Flight*>& flights)
{
    //declare local variables
    char choice = ' ';
    int sortCriteria = 1;
while (toupper(choice) != 'X')
{
    //choosing which field to sort the schedule by
    switch (sortCriteria)
    {
    case 1:
        sort (flights.begin(), flights.end(), Flight::SortByDepartCity);
        break;
    case 2:
        sort (flights.begin(), flights.end(), Flight::SortByDestinationCity);
        break;
    case 3:
        sort (flights.begin(), flights.end(), Flight::SortByDepartTime);
        break;
    case 4:
        sort (flights.begin(), flights.end(), Flight::SortByArrivalTime);
        break;
    case 5:
        sort (flights.begin(), flights.end(), Flight::SortByFlightNumber);
        break;
    case 6:
        sort (flights.begin(), flights.end(), Flight::SortByAircraftType);
        break;
    case 7:
        sort (flights.begin(), flights.end(), Flight::SortByFreqFlyPoints);
        break;
    case 8:
        sort (flights.begin(), flights.end(), Flight::SortByFlightFull);
        break;
    }


    //display header
    system("cls");
    cout << left << endl;
    cout << "     " << setw(7) << "From" << setw(6) << "To" << setw(8) << "Depart" << setw(8) << "Arrive" <<
        setw(8) << "Flight" << setw(12) << "Aircraft" << setw(12) << "Frequent" << setw(6) << "Flight\n";
    cout << "\t\t\t\t  " << setw(10) << "Number" << setw(8) << "Type" << setw(14) << "Flyer Points" << setw(6) << "Status\n";

    //slightly altering the header to indicate how the list is sorted
    switch (sortCriteria)
    {
    case 1:
        cout << "   --\\_/------------------------------------------------------------------\n\n";
        break;
    case 2:
        cout << "   ---------\\_/-----------------------------------------------------------\n\n";
        break;
    case 3:
        cout << "   ----------------\\_/----------------------------------------------------\n\n";
        break;
    case 4:
        cout << "   ------------------------\\_/--------------------------------------------\n\n";
        break;
    case 5:
        cout << "   --------------------------------\\_/------------------------------------\n\n";
        break;
    case 6:
        cout << "   -----------------------------------------\\_/---------------------------\n\n";
        break;
    case 7:
        cout << "   -----------------------------------------------------\\_/---------------\n\n";
        break;
    case 8:
        cout << "   ----------------------------------------------------------------\\_/----\n\n";
        break;
    }

    //step through the flights vector displaying the information
    for (int idx = 0; idx < flights.size(); idx++)
    {
        cout << "     " << setw(7) << flights[idx]->getDepartCity() << setw(6) << flights[idx]->getDestinationCity() << setw(8) <<
            flights[idx]->getDepartTime() << setw(9) << flights[idx]->getArrivalTime() << setw(10) << flights[idx]->getFlightNumber() << setw(11) <<
            flights[idx]->getAircraftType() << setw(11) << flights[idx]->getFreqFlyPoints();
        if (flights[idx]->getFlightFull())
            cout << setw(6) << "FULL\n\n";
        else
            cout << endl << endl;
        flights[idx]++;
    }

    //display footer
    cout << "   -----------------------------------------------------------------------\n\n";
    cout << "\t\t\t  C -- Change Sorting\n";
    cout << "\t\t\t  X -- Exit to Main Menu\n";
    cout << "\t\t\t  Enter C or X: ";

    //get choice from user
    cin >> choice;

    //error-trapping loop
    while ((toupper(choice) != 'C') && (toupper(choice) != 'X'))
    {
        cout << "Please choose \"C\" or \"X\": ";
        cin >> choice;
    }

    //changing the sort flag
    if (sortCriteria == 8)
        sortCriteria = 1;
    else
        sortCriteria++;     
}

}

And in my Flight.h file, I have these sort statements: 在我的Flight.h文件中,我有以下排序语句:

static bool SortByDepartCity(const Flight* f1, const Flight* f2)
{
    return f1->departCity < f2->departCity;
}

static bool SortByDestinationCity(const Flight* f1, const Flight* f2)
{
    return f1->destinationCity < f2->destinationCity;
}

static bool SortByDepartTime(const Flight* f1, const Flight* f2)
{
    return f1->departTime < f2->departTime;
}

static bool SortByArrivalTime(const Flight* f1, const Flight* f2)
{
    return f1->arrivalTime < f2->arrivalTime;
}

static bool SortByFlightNumber(const Flight* f1, const Flight* f2)
{
    return atoi(f1->flightNumber.c_str()) < atoi(f2->flightNumber.c_str());
}

static bool SortByAircraftType(const Flight* f1, const Flight* f2)
{
    return f1->aircraftType < f2->aircraftType;
}

static bool SortByFreqFlyPoints(const Flight* f1, const Flight* f2)
{
    return f1->freqFlyPoints > f2->freqFlyPoints;
}

static bool SortByFlightFull(const Flight* f1, const Flight* f2)
{
    return f1->flightFull < f2->flightFull;
}

remove this 删除这个

flights[idx]++; flight [idx] ++;

at the last line of printing loop. 在打印循环的最后一行。

PS Here's some code to simplify yours PS这里有一些代码可以简化您的代码

template< class T, class FieldType, FieldType T::*FieldPtr >
struct LessBy {
    bool operator()( const T * left, const T * right ) const {
        return left->*FieldPtr < right->*FieldPtr;
    }
};

typedef LessBy< Flight, std::string, & Flight::departCity > SortByDepartCity;
typedef LessBy< Flight, std::string, & Flight::destinationCity > SortByDestinationCity;
//and so on

This line that you do while printing: 您在打印时执行的以下行:

flights[idx]++;

will modify every pointer, almost certainly making each and every one invalid to dereference. 将修改每个指针,几乎可以肯定,使每个指针都无效以取消引用。
You should remove it. 您应该删除它。 (It looks like a line left over from older code.) (它看起来像是旧代码遗留下来的一行。)

It would also be a good idea to separate sorting from printing, as nobody expects an output function to modify the data it's printing. 将排序与打印分开也是一个好主意,因为没人期望输出函数会修改其打印数据。

And unless you have a very good reason, you shouldn't be working with pointers to flights, anyway. 而且,除非有充分的理由,否则无论如何都不应该使用指向飞行的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM