Discard qualifiers

J

Jim

Hi,
I'm trying to sort a vector using
sort(corelist.begin(),corelist.end());
so I've provided a method to overload the < operator in the file, like
so, but it isn't a class member and the code which uses it is in
another file containing a class which inherits from this class:

bool operator< ( const Core& a, const Core& b) {
return a.getdistance() < b.getdistance();
}

Only problem is I get a const discard qualifiers error, which seems
very annoying. It seems to make me choose whether I want to hide my
code's variables or have them const. Removing the const seems to allow
the program to compile but there are a lot of errors (top line of
which shown below). getdistance just returns a double and I'm using
gcc. I haven't got a == operator in the code, but think I may possibly
need one.

Any help would be gratefully received
James

/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/bits/stl_algo.h:
In function 'void std::partial_sort(_RandomAccessIterator,
_RandomAccessIterator, _RandomAccessIterator) [with
_RandomAccessIterator = __gnu_cxx::__normal_iterator<Core*,
std::vector<Core, std::allocator<Core> > >]':
 
V

Victor Bazarov

Jim said:
Hi,
I'm trying to sort a vector using
sort(corelist.begin(),corelist.end());
so I've provided a method to overload the < operator in the file, like
so, but it isn't a class member and the code which uses it is in
another file containing a class which inherits from this class:

bool operator< ( const Core& a, const Core& b) {
return a.getdistance() < b.getdistance();

Make sure 'getdistance' member is 'const'. If it doesn't change
the object for which it's called, it should be 'const'. If it does
change the object, then perhaps you need to review your design.

V
 
R

Rolf Magnus

Jim said:
bool operator< ( const Core& a, const Core& b) {
return a.getdistance() < b.getdistance();
}

Only problem is I get a const discard qualifiers error, which seems
very annoying.
getdistance just returns a double

Is it const then?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top