在映射的类中定义运算符< (c++)
Defining the operator < in a class for a map (c++)
我有一个class:
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;
}
当我编译时,它给了我一个错误:
"将'const Time'作为std::string Time::see_day()的'this'参数传递错误"放弃限定符[-fpermissive].
我想用它来创建一个迭代器,以升序写所有的map。我做错了什么?
你需要标记你的
Time::see_day() const
// ^^^^^
// needs to be const
与
行相同return day < t1.see_day();
试图在const
实例(本例中为t1
)上调用非const
成员函数,这是被禁止的,因此出现错误。
与Time::see_hour()
相同的问题(您也有一个错字,缺少函数调用的括号)。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- 如何显式调用运算符<<
- 模板操作员&lt;未打电话
- C / CUDA中的模板方法是3个角括号(&lt;&lt;&lt;)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 错误:调用"std::vector<:vector<int>>::p ush_back(std::vector<std::__cxx11::basic_string<
- C 建造者Clang STD :: Sill,找不到超载的操作员&lt;
- 为什么STD :: MAP需要操作员&lt;以及我如何写一个
- 为什么“操作员”需要const但不是为“运营商&lt;”
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 左角支架解释为操作员&lt;而不是模板参数
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- 超载操作员&lt;&lt; - 必须是二进制操作员
- 没有匹配的“运营商&lt;&lt;”