templated function problem

This is a discussion on templated function problem within the C++ Programming forums, part of the General Programming Boards category; why do I get the error cannot deduce template argument for Comparable? I don't see where i went wrong (duh). ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1
    why do I get the error cannot deduce template argument for Comparable? I don't see where i went wrong (duh). Comparator comp is a functor used to compare two items while sorting.

    Code:
    template <class Iter, class Comparable, class Comparator>
    void insertionSort( Iter begin, Iter end, Comparator comp)
    {
        for(Iter p = begin+1; p != end; p++ )
        {
            Comparable tmp = *p;
            Iter j;
    
            for( j = p; j != begin && comp(tmp, *(j-1)); --j )
                *j = *(j - 1);
          *j = tmp;
        }
    }
    
    
    // My main call looks like this. assume that everything prior is done right
    // including declarations of variables. LessThanByIDnum() is the functor that im trying
    // to use
    
       insertionSort(people.begin(), people.end(), LessThanByIDnum() );
    What am I doing wrong?

    edit:

    Well now i see that the compiler will not recognize what type Comparable should be. What is a reasonable way to solve this problem?

    Thanks in advance

    btw, im trying to work with maps.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    You have to use iterator_traits. Like this:

    Code:
    #include<iterator>
    
    template <class Iter, class Comparator>
    void insertionSort( Iter begin, Iter end, Comparator comp)
    {
        for(Iter p = begin+1; p != end; p++ )
        {
            typename iterator_traits<Iter>::value_type tmp = *p;
            Iter j;
    
            for( j = p; j != begin && comp(tmp, *(j-1)); --j )
                *j = *(j - 1);
          *j = tmp;
        }
    }
    Search for std::iterator_traits for details. You may also need to write a template specialization if you use your own iterator types.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quote Originally Posted by moktail View Post
    Well now i see that the compiler will not recognize what type Comparable should be. What is a reasonable way to solve this problem?
    As King Mir pointed out what you are really trying to get is the value_type from the iterator. As it is written here, Comparable is not deducible from any function argument so you have to specify it.

    In general, list all the deducible template parameters LAST in the template list. This allows you to specify a template instance giving only the non-deducible types and allow the compiler to deduce all the other types. For instance:

    Code:
    template <typename A, typename B>
    void my_function(B param)
    {
        ...
    }
    The compiler can deduce the type of B, but not A. When you invoke this template function, you need only specify the type of A, since B is deducible:

    Code:
    int b_value; // B is of type int
    // Call my_function using type char for A, and allow compiler to deduce that B == int
    my_function<char>(b_value);
    This only works if B comes last in the template parameter list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 03:28 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21