Sort fails for a struct I create

Hi,

I'm trying to learn to use the built-in sorting algorithm to sort vectors. I've successfully used sort for vectors of ints, floats, and so on. Now I'm trying to sort a vector of instances of a struct I created. The relevant code:

struct binary_board_state {
...
inline bool operator > (binary_board_state b2) { ...
inline bool operator < (binary_board_state b2) { ...
}
vector<binary_board_state> start;
sort( start.begin(), start.end() );

The < and > operators work properly, I've used them many times without problems. However, when I try to build, I get five near-identical errors:

/Developer/SDKs/MacOSX10.4u.sdk/usr/include/c++/4.0.0/bits/stl_algo.h:90: error: passing 'const binary_board_state' as 'this' argument of 'bool binary_board_state::operator<(binary_board_state)' discards qualifiers

/Developer/SDKs/MacOSX10.4u.sdk/usr/include/c++/4.0.0/bits/stl_algo.h:91: error: passing 'const binary_board_state' as 'this' argument of 'bool binary_board_state::operator<(binary_board_state)' discards qualifiers

/Developer/SDKs/MacOSX10.4u.sdk/usr/include/c++/4.0.0/bits/stl_algo.h:93: error: passing 'const binary_board_state' as 'this' argument of 'bool binary_board_state::operator<(binary_board_state)' discards qualifiers

/Developer/SDKs/MacOSX10.4u.sdk/usr/include/c++/4.0.0/bits/stl_algo.h:97: error: passing 'const binary_board_state' as 'this' argument of 'bool binary_board_state::operator<(binary_board_state)' discards qualifiers

/Developer/SDKs/MacOSX10.4u.sdk/usr/include/c++/4.0.0/bits/stl_algo.h:99: error: passing 'const binary_board_state' as 'this' argument of 'bool binary_board_state::operator<(binary_board_state)' discards qualifiers

I have no idea what these mean. Can someone else make sense of this?
Thanks in advance.
It is complaining that your operators have the potential to modify their arguments. They should be defined as:
1
2
  inline bool operator > ( const binary_board_state& b2 ) const { ... }
  inline bool operator < ( const binary_board_state& b2 ) const { ... }

This means that the argument may not be modified, and *this may not be modified.

Hope this helps.
It works perfectly now. Thank you, Duoas.
Topic archived. No new replies allowed.