为什么我有这个错误使操作员超载?

时间:2019-03-29 08:37:57

标签: c++

我想定义一个用于对对象“ esame”的向量进行排序的运算符,但是出现此错误:

  

/ * esame.cpp:46:6:错误:'bool的原型   esame :: operator <(esame&)const'与'esame'类中的任何内容都不匹配        布尔esame :: operator <(esame&exam)const             ^ ~~~~       在esame.cpp包含的文件中:1:0:* /       esame.h:20:10:错误:候选对象是:bool esame :: operator <(const esame&)const

     bool operator<(const esame &) const

    //this is the declaration on the header file:

    bool operator<(const esame &) const;





    //this is implementation on the cpp file:

    bool esame::operator<(esame &exam) const
    {
        if (this->getNome() < exam.getNome()){
            return true;
        } else{
            return false;
        }  
    }

2 个答案:

答案 0 :(得分:5)

签名不同。在实现中将const添加到esame &exam

答案 1 :(得分:0)

我照你说的做了,但是没用。现在我有这个错误:

esame.cpp:45:36:错误:将“ const esame”作为“ this”参数传递会丢弃限定符[-fpermissive]      如果(this-> _ nome