Home > Mobile >  no match for 'operator<' (operand types are 'const Vehicle' and 'const V
no match for 'operator<' (operand types are 'const Vehicle' and 'const V

Time:11-30

I have this class:

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

which has this implementation:

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

And I'm getting this error: "no match for 'operator<' (operand types are 'const Vehicle' and 'const Vehicle')"

which is in "stl_funxtion.h"

CodePudding user response:

To make a function usable on a const object, you need to declare that function const:

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

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