二进制“<”:'const_Ty' 未定义此运算符或转换为预定义运算符可接受的类型
Binary '<' : 'const_Ty' does not define this operator or a conversion to a type acceptable to the predefined operator
我在自己的代码中遇到了同样的错误。这是一个小例子。问题出在哪里?
#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
}
std::set
是一个 有序的 容器。对于自定义类型,您需要为您的类型提供排序。做到这一点的简单方法是为您的类型定义 operator<。
// return true if table x should go before table y in the set
bool operator<(const Table& x, const Table& y)
{
...
}
因为您没有这样做(或任何其他选项),所以您会收到错误消息。标准库不知道如何在集合中对您的表进行排序。
我在自己的代码中遇到了同样的错误。这是一个小例子。问题出在哪里?
#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
}
std::set
是一个 有序的 容器。对于自定义类型,您需要为您的类型提供排序。做到这一点的简单方法是为您的类型定义 operator<。
// return true if table x should go before table y in the set
bool operator<(const Table& x, const Table& y)
{
...
}
因为您没有这样做(或任何其他选项),所以您会收到错误消息。标准库不知道如何在集合中对您的表进行排序。