Okay so previously, I attempted to sort of create a generic type of program. I did this by using typedef to define two vectors. One of structs and one of doubles. However when I changed the typedef from vectors to list within my program, and all else the same, it happened to fail on me when I attempted to sort the list.
The error gives me something about a reverse_iterator. I don't recall reversing any iterator in my code.
Here is my main code
Code:
int main(){
typedef list<Student_info> students;
students studs;
Student_info record;
string::size_type maxlen = 0;
while(read(cin, record)){
maxlen = max(maxlen, record.name.size());
studs.push_back(record);
}
//problem is here!
sort(studs.begin(), studs.end(), compare);
for(students::iterator i = studs.begin(); i != studs.end(); ++i){
cout << (*i).name << string(maxlen + 1 - (*i).name.size(), ' ');
try{
double final_grade = grade(*i);
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec);
}
catch(domain_error e){
cout << e.what();
}
cout << endl;
}
return 0;
}
Here is the source file with the compare function
Code:
bool compare(const Student_info& x, const Student_info& y){
return x.name < y.name;
}
std::istream& read(std::istream& source, Student_info& student){
source >> student.name >> student.midterm >> student.final;
read_hw(source, student.hw);
return source;
}
std::istream& read_hw(std::istream& source, homework& hw){
double x;
if(source){
hw.clear();
while(source >> x){
hw.push_back(x);
}
source.clear();
}
return source;
}
Thank you for your help!