我正在使用gcc 4.3.3尝试编译以下代码:
struct testStruct {
int x;
int y;
bool operator<(testStruct &other) { return x < other.x; }
testStruct(int x_, int y_) {
x = x_;
y = y_;
}
};
int main() {
multiset<testStruct> setti;
setti.insert(testStruct(10,10));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:/
usr/include/c++/4.4/bits/stl_function.h|230| error:'__x <__y'中'operator <'不匹配
我怀疑我没有对运算符进行重载,因为它应该做,但我无法确切地指出确切的问题.我在这做错了什么?
小智 14
运算符必须是const并采用const引用:
bool operator<(const testStruct &other) const { return x < other.x; }
Run Code Online (Sandbox Code Playgroud)