【问题标题】:Custom class comparison needed for binary_search()binary_search() 需要自定义类比较
【发布时间】:2012-06-30 06:06:54
【问题描述】:

我有一个自定义类,std::vector 填充了这个类的对象。我想在这个数组中做binary_search

我在我的类上重载了这样的运算符:

bool operator ==(const someClass&);
bool operator > (const someClass&);
bool operator < (const someClass&);

他们工作正常(他们有身体,是的)。

现在我有一个错误错误

2   error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const someClass' (or there is no acceptable conversion)

我应该创建复制构造函数(已经重载 =,但没有帮助)还是向运算符添加其他内容?

谢谢。

【问题讨论】:

    标签: c++ stl-algorithm binary-operators


    【解决方案1】:

    你需要将操作符设为 const:

    bool operator < (const someClass&) const;
    

    没有它,只有 RHS 是 const。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-07
      • 1970-01-01
      • 2017-11-02
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      • 2018-12-22
      相关资源
      最近更新 更多