C++ 定义操作员<;在映射的类中(c+;+;)
所以我有一门课:C++ 定义操作员<;在映射的类中(c+;+;),c++,dictionary,C++,Dictionary,所以我有一门课: class Time{ private: string day; string hour; public: //etc } 我在课堂上定义了: bool Time::operator <(const Time &t1) const { if (day!= t1.see_day()) { return day < t1.see_day(); } else retur
class Time{
private:
string day;
string hour;
public:
//etc
}
我在课堂上定义了:
bool Time::operator <(const Time &t1) const {
if (day!= t1.see_day()) {
return day < t1.see_day();
}
else return hour < t1.see_hour;
}
bool Time::operator您需要标记
Time::see_day() const
// ^^^^^
// needs to be const
与其他方面一样
return day < t1.see_day();
return day
您尝试在const
实例上调用非const
成员函数(在本例中为t1
),这是禁止的,因此会出现错误
时间也有同样的问题:请参见_hour()
(您还有一个打字错误,函数调用缺少括号)。您需要标记
Time::see_day() const
// ^^^^^
// needs to be const
与其他方面一样
return day < t1.see_day();
return day
您尝试在const
实例上调用非const
成员函数(在本例中为t1
),这是禁止的,因此会出现错误
Time::see_hour()@xampla没问题,这是一个经常出现的错误,您会一次又一次地看到;)