|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想要用下面的代码实现对生生总成绩的排序但是编译不通过
- vector<Student*> stu;
- bool cmp(Student* s1,Student* s2) //用来定义排序方式
- {
- return ((s1->Get_Sum_Score()) > (s2->Get_Sum_Score()));
- }
- void Sort_Class_Mates() { sort(stu.begin(), stu.end(), cmp); } //学生排名
复制代码
报出如下错误:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C3867 “Class::cmp”: 非标准语法;请使用 "&" 来创建指向成员的指针 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C3867 “Class::cmp”: 非标准语法;请使用 "&" 来创建指向成员的指针 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2672 “sort”: 未找到匹配的重载函数 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2672 “sort”: 未找到匹配的重载函数 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2780 “void std::sort(const _RanIt,const _RanIt)”: 应输入 2 个参数,却提供了 3 个 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
错误 C2780 “void std::sort(const _RanIt,const _RanIt)”: 应输入 2 个参数,却提供了 3 个 final D:\Learning&Materials\Programming\c++\作业\final\final\final\student.h 169
我向问下这种用法对普通数组可以,怎么我这样不行??
有没有什么解决办法
- #include <iostream>
- #include <algorithm>
- #include <ctime>
- #include <cstdlib>
- #include <vector>
- using namespace std;
- struct student
- {
- int score;
- };
- void print(const student *pStu) {
- cout << pStu->score << ' ';
- }
- bool cmp(const student *stu1, const student *stu2) {
- return stu1->score < stu2->score;
- }
- int main(int, const char **)
- {
- srand(time(nullptr));
- vector<student *> stu;
- student *stu1 = new student;
- student *stu2 = new student;
- student *stu3 = new student;
- stu1->score = rand() % 100;
- stu2->score = rand() % 100;
- stu3->score = rand() % 100;
- stu.push_back(stu1);
- stu.push_back(stu2);
- stu.push_back(stu3);
- for_each(begin(stu), end(stu), print);
- cout << endl;
- sort(begin(stu), end(stu), cmp);
- for_each(begin(stu), end(stu), print);
- cout << endl;
- delete stu1;
- delete stu2;
- delete stu3;
- stu1 = nullptr;
- stu2 = nullptr;
- stu3 = nullptr;
- return 0;
- }
复制代码
|
|