<分区>
我在自己的代码中遇到了同样的错误。这是一个小例子。问题出在哪里?
#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
}
<分区>
我在自己的代码中遇到了同样的错误。这是一个小例子。问题出在哪里?
#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)
{
...
}
因为您没有这样做(或任何其他选项),所以会出现错误。标准库不知道如何在集合中对您的表进行排序。
关于c++ - 二进制 '<' : 'const_Ty' does not define this operator or a conversion to a type acceptable to the predefined operator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65489524/