Reputation: 1
this is the string to compare, lastname+firstName and id comp is public, my sort method does not work, I am not able to find where the code fails, I am supposed to have the students listed in the order
comp=new char[strlen(lName)+strlen(fName)+strlen(id)+1];
this is my student constructor:
Student::Student(char * first, char * last, char *i,char * stand, int credit, double g, Date * d, Date * matricDate){
lName=last;
fName=first;
id=i;
standing=stand;
credits=credit;
gpa=g;
dob=d;
matDate=matricDate;
comp=new char[strlen(lName)+strlen(fName)+strlen(id)+1];
sprintf(comp,"%s%s%s",lName,fName,id);
};
this is the operator I overload
bool Student::operator<(const Student &second){
if(comp<second.comp){
return true;
}else{
return false;
}
};
I have the student list as:
vector<Student *> roster_list;
I add student like this:
void Roster::addStudent(Student *student){
roster_list.push_back(student);
};
here is my sort:
void Roster::mySort(){
sort(roster_list.begin(),roster_list.end());
};
my main:
#include<iostream>
#include"Roster.cpp"
#include "Date.cpp"
#include "Student.cpp"
using namespace std;
int main(){
Roster *r=new Roster();
Date *d=new Date(12,05,1987);
Student *st=new Student("Jason", "Sam", "124542", "sophomore", 34, 4.0, d,d);
Student *st2=new Student("Ahmad", "Khan", "23452", "freshman", 34, 4.0, d,d);
Student *st3=new Student("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
Student *st4=new Student("Andrew", "Rosenberg", "34523", "Senior", 34, 4.0, d,d);
Student *st5=new Student("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
r->addStudent(st);
r->addStudent(st2);
r->addStudent(st3);
r->addStudent(st4);
// Student 3 and 5 are same
// == operator check
if(*st3==*st5){
cout<<"Students are same \n";
}
// != overloading operator check
if(*st3!=*st4){
cout<<"Students are different\n";
}
cout<<"################Before Sorting###############\n";
r->toString();
cout<<"################After Sorting###############\n";
r->mySort();
r->toString();
cout<<"\n"<<st->getComp();
cout<<"\n"<<st3->getComp();
return 0;
}
OUTPUT:
Students are same
Students are different
################Before Sorting###############
Student Info:Jason Sam 124542 sophomore 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Ahmad Khan 23452 freshman 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Selam Can 23431 freshman 3
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Andrew Rosenberg 34523 Senior 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
################After Sorting###############
Student Info:Jason Sam 124542 sophomore 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Ahmad Khan 23452 freshman 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Selam Can 23431 freshman 3
Birth Date: December 5, 1987
Matric Date: December 5, 1987
Student Info:Andrew Rosenberg 34523 Senior 4
Birth Date: December 5, 1987
Matric Date: December 5, 1987
SamJason124542
CanSelam23431
I tried this
bool StudentSort(Student* lhs, Student* rhs) {
return (*lhs)<(*rhs);
}
void Roster::mySort(){
sort(roster_list.begin(),roster_list.end(),StudentSort);
};
still does not work, I get compilation error I have gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
this is the error I get after this try:
Roster.cpp: In member function ‘void Roster::mySort()’:
Roster.cpp:69: error: argument of type ‘bool (Roster::)(Student*, Student*)’ does not match ‘bool (Roster::*)(Student*, Student*)’
Upvotes: 0
Views: 547
Reputation: 2988
Your vector/list contains only Student*
, not Student
's.
So, you would need to provide Std::sort something that compares Student*
correctly.
As it stands, default sort will probably being sorting by memory location, not the semantic value of the pointed to object.
// assumes operator< above,which it doesnt.
bool StudentSortFn(Student* lhs, Student* rhs) {
return (*lhs)<(*rhs);
}
Of course, it turns out the original operator<
has much the same issue embedded in it. Its comparing char*
fields based on pointer values (memory addresses), not on semantic contents.
It need to be rewritten to use strcmp or something similar like:
bool Student::operator<(const Student &second){
return strcmp(comp,second.comp)<0;
};
Upvotes: 2
Reputation: 87997
It's hard to sort Students when your vector is a vector of pointers. Simple way is to rewrite without pointers
vector<Student> roster_list;
void Roster::addStudent(Student student){
roster_list.push_back(student);
};
int main(){
Roster r;
Date d(12,05,1987);
Student st("Jason", "Sam", "124542", "sophomore", 34, 4.0, d,d);
Student st2("Ahmad", "Khan", "23452", "freshman", 34, 4.0, d,d);
Student st3("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
Student st4("Andrew", "Rosenberg", "34523", "Senior", 34, 4.0, d,d);
Student st5("Selam", "Can", "23431", "freshman", 34, 3.0, d,d);
r.addStudent(st);
r.addStudent(st2);
r.addStudent(st3);
r.addStudent(st4);
etc. etc.
Upvotes: 1