繁体   English   中英

尝试插入 std::set 时“不匹配 'operator<'”

[英]“no match for 'operator<'” when trying to insert to a std::set

我正在使用 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; }  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM