我有一個向量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;
}
TL; DR。你確定矢量中的所有指針都是有效的嗎? – juanchopanza
如果其中一個不合法,你會得到未定義的行爲。該矢量圍繞一堆內存地址移動,就是這樣。你不會期望一個整數矢量來改變它的元素。 – Ben
腐敗,也許? – Ben