tags:

views:

86

answers:

3

I have written those few line:

#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;

template <class T> struct First
{
    T num;

    First() {}

    First(const T &a) : num(a) {}
};

 template <typename var> bool criterio(First<var> &primo, First<var> &secondo)
 {
    return (primo.num < secondo.num);
 }

int main()
 {
    vector< First<int> > f;

    srand (time(NULL));
    for(int i=0; i<20; i++) f.push_back( First<int>(rand() % 20) );

    sort(f.begin(),f.end(),criterio);

    return 0;
 }

I compile with "g++ program2.C" and the answer is:

program2.C: In function ‘int main()’:

program2.C:28: error: no matching function for call to‘sort(__gnu_cxx::__normal_iterator*, std::vector, std::allocator > > > , __gnu_cxx::__normal_iterator*, std::vector, std::allocator > > >, unresolved overloaded function type)’

I have no idea of what kind of problem it is... Can you help me??

thanks for help...

+2  A: 
sort(f.begin(),f.end(),criterio<int>);

You need to explicitly state the function you're using.

Kornel Kisielewicz
+5  A: 

criterio is a template so you need to give the type it is templated on:

   sort(f.begin(),f.end(),criterio<int>)

and the criterio function must take const references as parameters:

 template <typename var> bool criterio(const First<var> &primo, 
                                         const First<var> &secondo)
  {
     return (primo.num < secondo.num);
  }
anon
It should but it musn't?
Kornel Kisielewicz
@Kornel Sorry, I don't get you.
anon
@Neil, disregard that -- it's just the same old story that MSVC is a non-standard conforming compiler (works without const) :>. Comenau online properly treats the lack of const as an error.
Kornel Kisielewicz
A: 

THANKS A LOT!!! Now my program RUN!!!

massimo
This is not a message board. Don't reply with non-answers. Instead mark the answer that helped you most as the solution.
UncleBens