【问题标题】:Binary '<' : 'const_Ty' does not define this operator or a conversion to a type acceptable to the predefined operator [duplicate]二进制“<”:“const_Ty”未定义此运算符或转换为预定义运算符可接受的类型[重复]
【发布时间】:2021-04-05 22:16:20
【问题描述】:

我在自己的代码中遇到了同样的错误。这是一个小例子。问题出在哪里?

#include <set>
using namespace std;

class Table{};

struct MyStruct
{
    set<Table> arr;
};

int main()
{
    MyStruct a;
    Table t;
    a.arr.insert(t); // Here it gives C2676 error
}

【问题讨论】:

    标签: c++ class struct c++14


    【解决方案1】:

    std::set 是一个有序容器。对于自定义类型,您应该为您的类型提供排序。简单的方法是为您的类型定义 operator

    // return true if table x should go before table y in the set
    bool operator<(const Table& x, const Table& y)
    {
        ...
    }
    

    因为您没有这样做(或任何其他选项),所以您会收到错误消息。标准库不知道如何对集合中的表进行排序。

    【讨论】:

      猜你喜欢
      • 2020-08-10
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2021-06-11
      • 2020-10-10
      • 1970-01-01
      • 2021-11-30
      • 2021-07-30
      相关资源
      最近更新 更多