我有這門課:
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”
uj5u.com熱心網友回復:
要使函式在const物件上可用,您需要宣告該函式const:
class Vehicle {
?
bool operator<(const Vehicle& v) const;
? ^^^^^
?
};
bool Vehicle::operator<(const Vehicle& v) const {
? ^^^^^
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369840.html
