1

I've tried to sort a vector of pairs, using approaches outlined in How do I sort a vector of pairs based on the second element of the pair?, but my compiler can't seem to resolve my templates.

The problem region looks approximately as follows:

template <typename T, typename Tds1, typename Tds2>
void myClass<T,Tds1,Tds2>::myMethod(myTemplateParam<T> &in, 
                                    myTemplateParam<T> &out) {

  typedef std::pair <Tds2, T> labelDistPair;
  std::vector< labelDistPair > labelRanks;

  struct sort_pairs {
    bool operator()(const std::pair<Tds2, T> &left,
                    const std::pair<Tds2, T> &right)
                    {return left.second < right.second;}
   };

  //...
  //Fill in labelRanks - a vector of <float, double> pairs
  //...


  std::sort(labelRanks.begin(), labelRanks.end(), sort_pairs());

  }
 }

The most relevant part of my error message seems to be:

/usr/include/c++/4.8/bits/stl_algo.h:5474:5: note: template argument deduction/substitution failed: ... error: trying to instantiate ‘template void std::sort(_RAIter, _RAIter, _Compare)’

Does this make sense? (I've gotten a similar error when I tried using a lambda function as the comparison function)

Community
  • 1
  • 1
user1245262
  • 6,968
  • 8
  • 50
  • 77
  • Are you sure that's your exact code? Looks fine to me. I put together a similar example and it compiled just fine. What argument are you giving for `Tds2`? – Anthony Mar 12 '14 at 02:37

1 Answers1

2

You're probably not compiling with a C++11-capable compiler (or you haven't enabled the feature). As seen in this question, C++03 doesn't allow a local type as a template argument (i.e. you need to move your sorting functor out into namespace scope.)

Community
  • 1
  • 1
Anthony
  • 12,177
  • 9
  • 69
  • 105