2014-04-16 74 views
0

我有一個向量Flight pointers,我傳遞到一個函數進行排序和顯示在屏幕上。我正在使用Flight class中的仿函數進行排序。它在第一次完美運行,然後在sortCriteria遞增後,它在第二次嘗試通過航班向量時崩潰。 我得到的錯誤說Access violation reading location 0x013DFFFC,所以我打賭它有一些事情是在第一次排序後丟失載體的內存位置。預先感謝您提供的任何幫助。 這裏是我的排序功能:排序指針向量一次然後崩潰與「無法讀取內存」

//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++;  
} 

} 

而且在我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; 
} 
+0

TL; DR。你確定矢量中的所有指針都是有效的嗎? – juanchopanza

+0

如果其中一個不合法,你會得到未定義的行爲。該矢量圍繞一堆內存地址移動,就是這樣。你不會期望一個整數矢量來改變它的元素。 – Ben

+0

腐敗,也許? – Ben

回答

2

刪除此

航班[IDX] ++;

在打印循環的最後一行。

P.S.下面是一些代碼來簡化你的

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 
+0

嗯......是啊。這是問題所在。也許我應該回顧一下循環的章節,提醒自己在將來不要增加一倍。非常感謝。 – Matt

0

這條線,你在打印過程中做到:

flights[idx]++; 

將修改每個指針,幾乎可以肯定,使每一個無效的取消引用。
你應該刪除它。 (它看起來像是從舊代碼中遺留的一條線)。

將排序與打印分開也是一個好主意,因爲沒有人希望輸出函數能夠修改它正在打印的數據。

除非你有很好的理由,否則你不應該使用指向航班的指針。