Thread: Template function as argument

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    722

    Red face Template function as argument

    I writed a Vector class of my own, like the standard one.
    I've haded it a sort method to sort the elements by a user defined funtion comparation, of using operator<.
    When compiling, I get this error:

    error C2896: 'void __cdecl quicksort(C *,int,int,int (__cdecl *)(const C &,const C &))' : cannot use function template 'int __cdecl defcmp(const C &,const C &)' as a function argument
    d:\...\vector.cpp(223) : see declaration of 'defcmp'

    Basicly, I think, that the compiler isn't associating the template in the quicksort function with the one from the defcmp function...
    How can I solve this??

    Code:
    /*.........*/
    template<class C>
    class Vector{
    private:
    	C*  _vector;
    	int _size;
    /*...............*/
    };
    
    template<class C>
    void quicksort(C* vec,int low, int high,int (*f_cmp)(const C&, const C&)){
    /*....................*/
    }
    
    template<class C>
    void Vector<C>::sort(int (*f_cmp)(const C&, const C&)){
    	quicksort(_vector,0,_size-1, f_cmp);
    }
    
    template<class C>
    int defcmp(const C& c1, const C& c2){
    	return (c1<c2) ? -1 : ( (c2<c1) ? 1 : 0);
    }
    
    template<class C>
    void Vector<C>::sort(){
    	quicksort(_vector,0,_size-1, defcmp);
    }
    I don't want to had the quicksort funtion to my class's scope, nor store the comparation function pointer in it.
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Bah... never mind

    Compiler Error C2896
    'function1' : cannot use function template 'function2' as argument

    A function template cannot be used as an argument to another function template. For example:

    template<class T1, class T2> void f1(void(*)(T1, T2));
    template<class T1, class T2> void f2(T1, T2);

    void g() {
    f1(f2);
    }

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    This might work:
    Code:
    template<class C>
    void Vector<C>::sort(){
    	quicksort(_vector,0,_size-1, defcmp<C>);
    }
    Just a thought
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Code:
    template<class C>
    void Vector<C>::sort(){
    	quicksort(_vector,0,_size-1, defcmp<C>);
    }
    Testing with int's...:

    d:\...\vector.cpp(232) : error C2782: 'void __cdecl quicksort(C *,int,int,int (__cdecl *)(const C &,const C &))' : template parameter 'C' is ambiguous
    could be 'const int &'
    or 'int'

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You might want to look into using functor style objects like less, instead of using an explicit function type.

    gg

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    don't forget the namespace

    Code:
    //int Vector::defcmp(......){}
    
    template<class C>
    int defcmp(const C& c1, const C& c2){
    	return (c1<c2) ? -1 : ( (c2<c1) ? 1 : 0);
    }

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    misplaced, good intentions but....
    (I kill me!).

    I believe that defcmp<>() was meant to be a generic template function, provided for the sole purpose of being the default comparison function when calling the Vector<>::sort() with no parameters.

    gg

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    oh...i didn't really look at the purpose of the code

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    I declared the sorting functions as friends and the comparison function pointer in the class. It work but it isn't really what I wanted. More 4 bytes to the class...

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It's better if you use functor objects instead.
    Code:
    #include <iostream>
    #include <vector>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    using namespace std;
    
    template <class T>
    class Vector
    {
        vector<T> v; 
    
    public:
        void add(const T &val) {v.push_back(val);}
    
        // sort using Comp functor
        template <class Comp>
        void sort(Comp c) {std::sort(v.begin(), v.end(), c);}
    
        // default sort using less<T> functor
        void sort() {sort(less<T>());}
    
        // ostream support
        friend ostream& operator << (ostream &out, const Vector<T> &vec)
        {
            copy(vec.v.begin(), vec.v.end(), ostream_iterator<T>(out, "\n"));
            
            return out;
        }//friend operator << (ostream, Vector<T>&)
    };//Vector
    
    bool my_int_greater(const int &lhs, const int &rhs) {return lhs > rhs;}
    
    int main()
    {
        Vector<int> vi;
    
        vi.add(5); vi.add(3); vi.add(7); vi.add(2); vi.add(9); vi.add(1);
    
        cout << "Unsorted:\n" << vi << endl;
    
        vi.sort();
        cout << "Sorted:\n" << vi << endl;
    
        vi.sort(my_int_greater);
        cout << "Re-Sorted:\n" << vi << endl;
    
        return 0;
    }//main
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM