Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 定义操作员<;在映射的类中(c+;+;)_C++_Dictionary - Fatal编程技术网

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没问题,这是一个经常出现的错误,您会一次又一次地看到;)