我正在使用 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;
}
我得到这个错误:
/usr/include/c++/4.4/bits/STL_function.h|230|错误:‘__x < __y’中的‘operator<’不匹配
我怀疑我没有像应该做的那样重载运算符,但我无法查明确切的问题。我在这里做错了什么?
最佳答案
运算符必须是 const 并且取一个 const 引用:
bool operator<(const testStruct &other) const { return x < other.x; }
关于c++ - “no match for ' operator< '” 尝试插入到 std::set 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214569/