文章标题

来源:互联网 发布:泰坦军团 显示器 知乎 编辑:程序博客网 时间:2024/06/05 06:20
1 : error: passing ‘const student’ as ‘this’ argument of ‘const int student::gets()’ discards qualifiers [-fpermissive]
class student{    privateint 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{};