这是要比较的字符串,lastname + firstName和id comp是公共的,我的排序方法不起作用,我无法找到代码失败的地方,我应该让学生在订单中列出
comp=new char[strlen(lName)+strlen(fName)+strlen(id)+1];
这是我的学生构造函数:
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);
};
这是我重载的运算符
bool Student::operator<(const Student &second){
if(comp<second.comp){
return true;
}else{
return false;
}
};
我有学生名单:
vector<Student *> roster_list;
我像这样添加学生:
void Roster::addStudent(Student *student){
roster_list.push_back(student);
};
这是我的排序:
void Roster::mySort(){
sort(roster_list.begin(),roster_list.end());
};
我的主要:
#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;
}
输出:
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
我试过这个
bool StudentSort(Student* lhs, Student* rhs) {
return (*lhs)<(*rhs);
}
void Roster::mySort(){
sort(roster_list.begin(),roster_list.end(),StudentSort);
};
仍然无法正常工作,我收到编译错误我有gcc版本4.4.7 20120313(Red Hat 4.4.7-3)(GCC)
这是我在尝试之后得到的错误:
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*)’
答案 0 :(得分:3)
您似乎已将bool StudentSort
作为Roster
的成员函数。你应该让它成为非会员。
另外,您应该制作operator<
const
,以便它可以对LHS和RHS上的const
个引用进行操作。比较运算符改变操作数是没有意义的。您也可以简化它:
bool Student::operator<(const Student& second) const {
return comp<second.comp;
}
有了这个,您可以更改比较函数以指向const
:
bool StudentSort(const Student* lhs, const Student* rhs) { .... }
修改由于@RetiredNinja已指针指出,Student::comp
为char*
,因此您的比较运算符正在比较指针。这不太可能是你想要的。解决这个问题和你的大多数其他问题的方法是根本不使用指针。特别是如果你不明白它们是什么。我建议std::string
使用comp
。