-3

可能重复:
与自己的班级交友“>>”

我正在尝试将 '<' 运算符与我自己的类交朋友,但它没有注意到我的语法错误,但我得到了以下编译错误:

错误:

**** Internal Builder is used for build               ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o main.o ..\main.cpp
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/algorithm:63:0,
                 from ..\main.cpp:8:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h: In function '_RandomAccessIterator std::__unguarded_partition(_RandomAccessIterator, _RandomAccessIterator, const _Tp&) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<RAngle*, std::vector<RAngle> >, _Tp = RAngle]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h:2253:70:   instantiated from '_RandomAccessIterator std::__unguarded_partition_pivot(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<RAngle*, std::vector<RAngle> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h:2284:54:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<RAngle*, std::vector<RAngle> >, _Size = int]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h:5330:4:   instantiated from 'void std::sort(_RAIter, _RAIter) [with _RAIter = __gnu_cxx::__normal_iterator<RAngle*, std::vector<RAngle> >]'
..\main.cpp:13:24:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_algo.h:2215:4: error: passing 'const RAngle' as 'this' argument of 'bool RAngle::operator<(RAngle)' discards qualifiers [-fpermissive]

类和重载函数:

class RAngle
{
public:
    int x,y,l; 
    int solution,prec;
    RAngle(){
    }

    RAngle(int i,int j,int k){
        x=i,y=j,l=k;
    }

    bool operator<(const RAngle rhs)
    {
        if(l < rhs.l){
            return true;
        }
    return 0;
    }
};

是否出现错误(main.cpp):

void descrSort(vector <RAngle> &l){
    sort(l.begin(),l.end());
    reverse(l.begin(),l.end());

    for(unsigned i=0; i<l.size();i++){
        cout<<l[i]<<endl;
    }

}
4

1 回答 1

3

这不能被添加到你关于这个主题的最后一个问题中吗?

bool operator<(const RAngle rhs)

应该

bool operator<(const RAngle& rhs) const

这应该修复错误。将不修改状态的方法标记为 是一个好习惯const

于 2012-06-13T11:55:43.050 回答