|  | 
 
| 
测试代码如下:
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 复制代码#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
struct fun
{
        int a;
        int b;
        
        fun()
        {
                a=0;
                b=0;
        }
        
        bool operator < (fun sum)    
        {
                return b < sum.b;
        }
};   //定义结构提  
int main()
{
        fun s[20]={};
        for(int i=1;i<=10;i++)  //对结构体变量进行赋值
        {
                s[i].a=rand()%20;
                s[i].b=rand()%20;
        }
        
        sort(&s[1],&s[11]);  //排序
        return 0;
}
 
 
 错误信息:
 
 复制代码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<(fun)’ discards qualifiers
/usr/include/c++/4.4/bits/stl_algo.h:90: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator<(fun)’ discards qualifiers
/usr/include/c++/4.4/bits/stl_algo.h:92: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator<(fun)’ discards qualifiers
/usr/include/c++/4.4/bits/stl_algo.h:96: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator<(fun)’ discards qualifiers
/usr/include/c++/4.4/bits/stl_algo.h:98: error: passing ‘const fun’ as ‘this’ argument of ‘bool fun::operator<(fun)’ discards qualifiers
 
 
 
 
 
 
 | 
 |