-3

Possible Duplicate:
Friending '>>' with own class

I'm trying to friend '<' operator with my own class but it doesn't notice me about an syntax error, but i get the fallowing compile error:

error:

**** 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 and overload function:

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;
    }
};

Were error appears(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;
    }

}
Community
  • 1
  • 1
Bogdan M.
  • 2,161
  • 6
  • 31
  • 53

1 Answers1

3

Couldn't this have been added to your last question on this subject?

bool operator<(const RAngle rhs)

should be

bool operator<(const RAngle& rhs) const

which should fix the error. It is a good habit to mark methods which do not modify state as const.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
Rook
  • 5,734
  • 3
  • 34
  • 43
  • 1
    For a longer explanation: inside `std::sort` values at different positions are compared. An element is selected as the pivot and a function that partitions the range into those elements smaller/larger than the value is called. That function takes the value used for the partition by constant reference, and tries to call your `operator<`. When the constant reference is used as the left hand side of the expression, your `operator<` cannot be used, as the function is not const, and calling a non-const function (from the point of view of the compiler) could change the `const` reference. – David Rodríguez - dribeas Jun 13 '12 at 12:07
  • Oops, didn't spot the missing reference sigil. Ta. – Rook Jun 13 '12 at 12:11
  • not really it has been told me to make it to bool from int function, but thank you for the answere – Bogdan M. Jun 13 '12 at 12:25