0

I am trying to make a template that compares the second item of pairs:

template <class T>
bool sortPairKey2(T u,  T v) { return u.second < v.second;}

this way if I have pairs of pairs, I can still use the function.

But when I use it in the sort method (from ) I get:

error: no matching function for call to ‘sort(std::vector<std::pair<int, int> >::iterator, std::vector<std::pair<int, int> >::iterator, <unresolved overloaded function type>)’
     sort(sums.begin(), sums.end(), sortPairKey2<iipair,iipair>);
                                                               ^
stp2mzn.cpp:409:63: note: candidates are:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from stp2mzn.cpp:9:
/usr/include/c++/4.8/bits/stl_algo.h:5447:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
     sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
     ^
/usr/include/c++/4.8/bits/stl_algo.h:5447:5: note:   template argument deduction/substitution failed:
stp2mzn.cpp:409:63: note:   candidate expects 2 arguments, 3 provided
     sort(sums.begin(), sums.end(), sortPairKey2<iipair,iipair>);
                                                               ^
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from stp2mzn.cpp:9:
/usr/include/c++/4.8/bits/stl_algo.h:5483:5: note: template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)
     sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
     ^
/usr/include/c++/4.8/bits/stl_algo.h:5483:5: note:   template argument deduction/substitution failed:
stp2mzn.cpp:409:63: note:   couldn't deduce template parameter ‘_Compare’
     sort(sums.begin(), sums.end(), sortPairKey2<iipair,iipair>);

where typedef pair<int,int> iipair.

the function worked fine when instead of T I had pair<int,int>. I dont see what its problem is.

Thank you!

excalibur1491
  • 1,201
  • 2
  • 14
  • 29
  • Why not use lambda expression? – xuhdev Jul 25 '15 at 08:31
  • If you have made a regular function into a template function it can also be due to http://stackoverflow.com/questions/22595015/c-class-template-undefined-reference-to-function/26816994#26816994 – v010dya Jul 25 '15 at 08:38
  • 1
    Even though you've already received two answers I believe to be correct, it would have been nice if you had posted the full code, i.e. including the *call* to the `std::sort` function, not just the functor itself. – Christian Hackl Jul 25 '15 at 09:44
  • @xuhdev: I dont use C++11 (my code is part of a bigger project and it seems like no one uses c++11 in it, I don't want to make stuff complicated in terms of linkage etc lol) – excalibur1491 Jul 26 '15 at 01:23
  • @Volodya thanks for that. Wasn't that, but its good to know! – excalibur1491 Jul 26 '15 at 01:23
  • @ChristianHackl well, the call is in the error message printed by g++.... – excalibur1491 Jul 26 '15 at 01:24

2 Answers2

4

Please try sortPairKey2<iipair> instead of sortPairKey2<iipair,iipair>

nkdm
  • 1,220
  • 1
  • 11
  • 26
3

Two things.

  1. you should to pass the parameter by const reference
  2. don't write sortPairKey2<iipair, iipair>, either get rid of the template specification completely (your compiler should figure it out), or just write sortPairKey2<iipair>
roalz
  • 2,699
  • 3
  • 25
  • 42
hellow
  • 12,430
  • 7
  • 56
  • 79