| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Jim" |
| Date: |
24 Aug 2007 12:54:20 PM |
| Object: |
Discard qualifiers |
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> > >]':
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: Discard qualifiers |
24 Aug 2007 12:57:45 PM |
|
|
Jim wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: Discard qualifiers |
24 Aug 2007 02:12:32 PM |
|
|
Jim wrote:
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?
.
|
|
|
|

|
Related Articles |
|
|