1 : error: passing ‘const student’ as ‘this’ argument of ‘const int student::gets()’ discards qualifiers [-fpermissive]
class student{
private:
int score;
public:
int gets(){
return score;
}
}
bool cmp(const student &a,const student &b){
return a.gets()>b.gets(); //error: passing 'const student' as 'this' argument of 'const int student::gets()' discards qualifiers [-fpermissive]
}
错误原因:
a,b是const对象,gets()成员函数不能保证不对对象进行修改,编译器做了一个安全设定认为这个成员函数会对对象成员修改。
解决办法:
函数加入const 限定符
int gets() const{};