加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

尝试插入到(c)中时,’operator <'不匹配?

发布时间:2020-12-16 03:37:44 所属栏目:百科 来源:网络整理
导读:我使用 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() {multisettestStruct setti; setti.insert(testStruct(10,10
我使用 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|error:’__x< __y”
我怀疑我没有做这个操作符重载,因为它应该做,但我只是无法确定确切的问题.我在这里做错了什么?

解决方法

操作符必须是const并引用const引用:
bool operator<(const testStruct &other) const { return x < other.x; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读