Jump to content
  • Advertisement
Sign in to follow this  
thedustbustr

std::list::sort(functor) issues

This topic is 5459 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to sort my std::list<System*> 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?

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!