c语言 结构体重载运算符,为什么我重载了<运算符,但还是不能使用sort对结构体进行排序?...

这篇博客讲述了在使用C++4.4版本的STL算法库时遇到的问题,关于在调用`std::sort`函数时,由于传入了常量fun对象,导致函数试图将const fun作为非const this参数,引发编译错误。详细探讨了问题的原因和解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In file included from /usr/include/c++/4.4/algorithm:62,

from test.cpp:2:

/usr/include/c++/4.4/bits/stl_algo.h: In function ‘const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&) [with _Tp = fun]’:

/usr/include/c++/4.4/bits/stl_algo.h:2268:   instantiated from ‘void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = fun*, _Size = int]’

/usr/include/c++/4.4/bits/stl_algo.h:5220:   instantiated from ‘void std::sort(_RAIter, _RAIter) [with _RAIter = fun*]’

test.cpp:32:   instantiated from here

/usr/include/c++/4.4/bits/stl_algo.h:89: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator

/usr/include/c++/4.4/bits/stl_algo.h:90: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator

/usr/include/c++/4.4/bits/stl_algo.h:92: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator

/usr/include/c++/4.4/bits/stl_algo.h:96: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator

/usr/include/c++/4.4/bits/stl_algo.h:98: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值