我有一个向量Flight pointers
,我将其传递到一个函数中,以便在屏幕上进行排序和显示。我在Flight class
中使用一个函子来进行排序。它第一次工作得很好,然后在sortCredential增加后,当它第二次尝试遍历飞行矢量时崩溃。我得到的错误是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) << "Flightn";
cout << "tttt " << setw(10) << "Number" << setw(8) << "Type" << setw(14) << "Flyer Points" << setw(6) << "Statusn";
//slightly altering the header to indicate how the list is sorted
switch (sortCriteria)
{
case 1:
cout << " --\_/------------------------------------------------------------------nn";
break;
case 2:
cout << " ---------\_/-----------------------------------------------------------nn";
break;
case 3:
cout << " ----------------\_/----------------------------------------------------nn";
break;
case 4:
cout << " ------------------------\_/--------------------------------------------nn";
break;
case 5:
cout << " --------------------------------\_/------------------------------------nn";
break;
case 6:
cout << " -----------------------------------------\_/---------------------------nn";
break;
case 7:
cout << " -----------------------------------------------------\_/---------------nn";
break;
case 8:
cout << " ----------------------------------------------------------------\_/----nn";
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) << "FULLnn";
else
cout << endl << endl;
flights[idx]++;
}
//display footer
cout << " -----------------------------------------------------------------------nn";
cout << "ttt C -- Change Sortingn";
cout << "ttt X -- Exit to Main Menun";
cout << "ttt 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;
}
删除此
航班[idx]++;
在打印循环的最后一行。
附言:这里有一些简化的代码
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
打印时执行的此行:
flights[idx]++;
将修改每个指针,几乎可以肯定地使每个指针对取消引用无效
你应该删除它。(它看起来像是旧代码留下的一行。)
将排序与打印分离也是一个好主意,因为没有人期望输出函数修改正在打印的数据。
和除非你有充分的理由,否则你不应该使用航班指针。