5

Not sure if I have a simple typo somewhere, but I'm running into issues in sorting a deque of tuples.

So, my deque looks like this:

std::deque<boost::tuple<unsigned int, unsigned int> > messages;

And then I have my call to sort:

sort(messages.begin(), messages.end(), msg_sort_criteria);

And my sorting function:

bool msg_sort_criteria(boost::tuple<unsigned int, unsigned int> lhs, boost::tuple<unsigned int, unsigned int> rhs)
{
  return boost::get<1>(lhs) < boost::get<1>(rhs);
}

What happens is that I get errors in stl_heap.h and stl_algo.h. For instance,

Called object type '<bound member function type>' is not a function or function parameter.


Edit:

For clarification, this is all taking place within private members of a class.

class Messages::MessageImpl{
private:
  std::deque<boost::tuple<unsigned int, unsigned int> > messages;

  bool msg_sort_criteria(boost::tuple<unsigned int, unsigned int> lhs, boost::tuple<unsigned int, unsigned int> rhs)
  {
    return boost::get<1>(lhs) < boost::get<1>(rhs);
  }

  void fn()
  {
    sort(msg_queue_.begin(), msg_queue_.end(), msg_sort_criteria);
  }
}
erik
  • 3,810
  • 6
  • 32
  • 63
  • 1
    Is `msg_sort_criteria` a free function or a member function? If the former, you need to show more code; if the latter, make it `static` and change `sort(messages.begin(), messages.end(), msg_sort_criteria);` to `sort(messages.begin(), messages.end(), &myClassName::msg_sort_criteria);`. – ildjarn Jul 17 '12 at 18:00
  • All of this code is taking place within private members of a class. I'll update accordingly – erik Jul 17 '12 at 18:05
  • Are you intentionally only sorting by the first member of the `tuple`? If you don't care about the order of items where the first element is equal you can just use the default tuple `operator<` and don't bother with a sort predicate at all. – Mark B Jul 17 '12 at 18:10
  • The first element is going to be unique (it is an ID) and the second element is a timestep, which I'm attempting to sort by. – erik Jul 17 '12 at 18:12

1 Answers1

1

Mostly reposting from comment.

Change your implementation to:

class Messages::MessageImpl{
private:
  std::deque<boost::tuple<unsigned int, unsigned int> > messages;

  static bool msg_sort_criteria(boost::tuple<unsigned int, unsigned int> lhs,
                                boost::tuple<unsigned int, unsigned int> rhs)
  {
    return boost::get<1>(lhs) < boost::get<1>(rhs);
  }

  void fn()
  {
    sort(msg_queue_.begin(), msg_queue_.end(), &MessageImpl::msg_sort_criteria);
  }
};
ildjarn
  • 62,044
  • 9
  • 127
  • 211