Help! template function

I am trying to write a template function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

double max(vector<double>& x)
{
        double max_v ;
        max_v = *max_element(x.begin(),x.end());
}

double min(vector<double>& x)
{
        double min_v ;
        min_v = *min_element(x.begin(),x.end());
}




template <class T>
double value (T fun, vector<double>& x)
{
        double *res;
        transform(x.begin(),x.end(),res,(*fun));
        return *res;
}

int main()
{

        vector<double> x;
        double (*fun_pt)(vector<double>& ) = max;
        for (int i =1; i<=10; i++)
                x.push_back(i*10.);


        cout << (*fun_pt)(x) << endl;
        cout << value(fun_pt, x) << endl;
}


and I got some error messages:

/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function â:
fail.cc:27: instantiated from â
fail.cc:41: instantiated from here
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:925: error: invalid initialization of reference of type â from expression of type â

can anyone tell me how to make it right? Thanks!
Depends what you are trying to do.

The problem at the moment is that the function argument in the transform function is expecting a function that takes a double as an argument and you are passing it a function which takes a vector<double> as its argument, so it is complaining that the function (max in this case) cannot be initialized with a double e.g. x.begin()

So the question is, what did you intend to happen?
Topic archived. No new replies allowed.