BUG记录本

此段代码想对学生类以 ID 进行排序写了一个 CMP 函数,运行时在 sort 函数出现 “required from here” 错误(已将 CMP 函数重新写在类外,作为一个函数而非类的成员函数解决此问题),但仍然不知道出现此问题的原因,应该是对类的认识不足,稍微记录下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std ;

const int maxNumberOfStudent = 100005 ;

struct studentMeassage{
string m_id ;
string m_name ;
string m_sex ;
int m_age ;
string m_profession ;
};

class Student{
private:
int m_count ;
studentMeassage data[maxNumberOfStudent] ;
// vector<studentMeassage> data;
public:
Student();
void enterMeassage(studentMeassage newinfo) ;
void insertMeassage(studentMeassage newinfo) ;
void deleteMeassageByName(string name) ;
void modifyMeassageByName(string name) ;
void searchMeassageByProfession(string profession) ;
void displayMainMenu() ;
void displayMeassage(int index) ;
void displayAllMeassage() ;
bool displayMeassageByID(int index , string id , bool select_is_called) ;
void sortByID() ;
bool cmpByID(studentMeassage data_one , studentMeassage data_two) ;
void saveInsideTxt() ;
/*
extended function
*/
};

void Student::displayAllMeassage(){
for (int i = 0 ; i < m_count ; i ++){
displayMeassage(i) ;
}
return ;
}

bool Student::cmpByID(studentMeassage data_one , studentMeassage data_two){
if (data_one.m_id.length() == data_two.m_id.length()){
return data_one.m_id < data_two.m_id ;
}
return data_one.m_id.length() < data_two.m_id.length() ;
}

void Student::sortByID(){
//此处 cmpByID 处出现 "requird from here" 错误
sort(data , data + m_count , cmpByID) ;
return ;
}