c++ - 使用带有 operator== 的 std::find 时出错

标签 c++ std

我在以下结构上使用 std::find 时遇到错误...

struct ComplianceOrderRecord {
    explicit ComplianceOrderRecord(IOrder& order);
    bool operator ==(const ComplianceOrderRecord& other) const;
    double price;
};

inline bool ComplianceOrderRecord::operator ==(const ComplianceOrderRecord& other) const {
    return price == other.price;
}

我是这样使用的...

inline void Compliance::RemoveComplianceOrderRecord(const ComplianceOrderRecord& order) {
    auto it = std::find(m_compliantOrderList.begin(),
    m_compliantOrderList.end(), order);
    if(it == m_compliantOrderList.end()) {
        return;
    }
    m_compliantOrderList.erase(it);
}

错误是...

error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const ComplianceOrderRecord' (or there is no acceptable conversion)

如能帮助理解此错误,我们将不胜感激。

最佳答案

您的 operator== 应该是一个 const 成员,或者更好的是,一个独立函数。

关于c++ - 使用带有 operator== 的 std::find 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8852034/

相关文章:

带有 TensorRT 的 C++ Tensorflow API

c++ - 为提升的小部件设置父 QWidget

c++ - 如何在 C++ 中迭代字符串和结构的映射?

c++ - boost::regex vs std::regex - 找不到 empty() 方法?

c++ - 新 arg 在不同的 arg 上生成错误

c++ - 使用 union 写入位边界

c++ - UUID生成器问题

c++ - 指向所有 std::vector 元素的指针

c++ - 整数的快速集合并集

c++ - std::vector::reserve 失效