Skip to main content
gamedev.net
🔒 Locked

std::list::sort(functor) issues

Started by thedustbustr Oct 12, 2004 at 2:45 PM 0 replies 2.2k views
Original Post
thedustbustr
thedustbustr
I want to sort my std::list by priority. If I std::sort it it will sort by address so I'm trying to ovvreride this however possible. I tried three ways and they all didnt work.
class System{
public:
    int priority;
    bool operator<(const System& system) const
    {return (priority < system.priority)?true:false;}
}; 
*************************
inline bool operator<(const System* a, const System* b) const
{ return ((*a) < (*b)); }
systems.sort();
error C2270: '<' : modifiers not allowed on nonmember functions error C2803: 'operator <' must have at least one formal parameter of class type *************************
inline bool System_sort_1(const System* a, const System* b) const
{ return (*a < *b); }
systems.sort(System_sort_1);
error C2270: 'System_sort_1' : modifiers not allowed on nonmember functions *************************
struct System_sort_2 : public std::binary_function < const System* const, const System* const, bool >
{
	bool operator()(const System* const a, const System* const b) const
	{ return (*a < *b); }
};
systems.sort(System_sort_2);
error C2275: 'System_sort_2' : illegal use of this type as an expression What gives?
Enigma
Enigma
Number 1 fails for two reasons. You can't overload an operator purely on pointer types, plus a non-member function can't be const because it has no state to maintain:
inline bool operator<(const System* a, const System* b) const{ return ((*a) < (*b)); }


Number 2 fails because a non-member function can't be const - see above:
inline bool System_sort_1(const System* a, const System* b) const{ return (*a < *b); }


Number 3 fails because you try to pass the class when you need to pass an instance of the class:
systems.sort(System_sort_2);


Try number 3, but with:
systems.sort(System_sort_2());


Enigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.