448,825 Members | 1,625 Online Need help? Post your question and get tips & solutions from a community of 448,825 IT Pros & Developers. It's quick & easy.

# templated function as parameter of another templated function

 P: n/a I have a bunch of templated functions: template double f2(Type_t x) { return 2*x; } template double f3(Type_t x) { return 3*x; } and a wrapper function, also templated, which I want to take f2 and f3 as arguments: template double foobar(Type_t x) { double y=0, z=0; // compute something complicated from x first, // the same for all function pointers f. z = 4*x; // over-simplification here. // main computation. y=f(z); return y; } Then, void some_function(){ double x=5, y=0; y=foobar(x); cout << y << endl; y=foobar(x); cout << y << endl; } works just fine. The problem is what happens when all these are members of a class: class A { public: template double foobar(Type_t x) { double y=0, z=0; // compute something complicated from x first, // the same for all function pointers f. z = 4*x; // over-simplification here. // main computation. y=f(z); return y; } template double f2(Type_t x) { return 2*x; } template double f3(Type_t x) { return 3*x; } void some_function(){ double x=5, y=0; y=foobar(x); cout << y << endl; y=foobar(x); cout << y << endl; } }; This doesn't compile. With g++-4.0.2 I get these errors: g++ -g -Wall -o templtemplfunc templtemplfunc.C templtemplfunc.C: In member function 'void A::some_function()': templtemplfunc.C:39: error: no matching function for call to 'A::foobar(double&)' templtemplfunc.C:42: error: no matching function for call to 'A::foobar(double&)' Compilation exited abnormally with code 1 at Tue Jul 4 13:01:55 How exactly do I call A::foobar() from within A::some_function()? Is what I want to do possible in the first place? Thanks! Jul 4 '06 #1 