'operator<' 不匹配(操作数类型为 'const Vehicle' 和 'const Vehicle')

no match for 'operator<' (operand types are 'const Vehicle' and 'const Vehicle')

我有这个class:

class Vehicle {
    private:
        char dir_;
    public:
        char car_;
        // functions
        // 
        // overloaded operators
        bool operator==(Vehicle&);
        bool operator<(const Vehicle& v);
        //
        // other functions
    
};

具有以下实现:

bool Vehicle::operator<(const Vehicle& v) {
    return (car_ < v.car_);
}

我收到了这个错误: “'operator<' 不匹配(操作数类型为 'const Vehicle' 和 'const Vehicle')”

在“stl_funxtion.h”

要使函数在 const 对象上可用,您需要声明该函数 const:

class Vehicle {
      ⋮
    bool operator<(const Vehicle& v) const;
      ⋮                              ^^^^^
      ⋮
};

bool Vehicle::operator<(const Vehicle& v) const {
      ⋮                                   ^^^^^
}