I'm not used to C++ templates, and I have a problem using the libraries made with templates.
These are math-related libraries included in Numerical Methods in C 3rd edition, and I got a problem with using the methods.
Below is the function that I'm trying to use:
// roots.h
//...
template <class T>
// Doub is a defined type that is equal to double
Doub rtbis(T &func, const Doub x1, const Doub x2, const Doub xacc) {
const Int JMAX=50;
Doub dx,xmid,rtb;
Doub f=func(x1);
Doub fmid=func(x2);
if (f*fmid >= 0.0) throw("Root must be bracketed for bisection in rtbis");
rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2);
for (Int j=0;j<JMAX;j++) {
fmid=func(xmid=rtb+(dx *= 0.5));
if (fmid <= 0.0) rtb=xmid;
if (abs(dx) < xacc || fmid == 0.0) return rtb;
}
throw("Too many bisections in rtbis");
}
//...
And with this, in main.cpp I tried to call
// main.cpp
#include "nr3.h"
#include "roots.h"
#include "bessel.h"
int main() {
Doub (Bessjy::*fptr) (Doub) = &Bessjy::j0;
Doub root = 0;
root = rtbis<double (Bessjy::*)(double)>(fptr,1.0,10.0,0.000001);
std::cout << root << std::endl;
}
I used g++ to compile, and the error message saids:
error: called object type 'double (Bessjy::*)(double)' is not a function or function pointer
Doub f=func(x1);
note: in instantiation of function template specialization
'rtbis<double (Bessjy::*)(double)>' requested here
root = rtbis<double (Bessjy::*)(double)'>(fptr,1.0,10.0,0.000001);
I modified the code as the compiler told me to do, but still it repeats the same error message.
I don't get how to solve it. Certainly I'm missing the important syntax matter.
Bessjy::j0astaticfunction?Bessjy::j0()withx1as the first argument on the line corresponding to the error message. However, since this is a member function (probably not a static based on the error message), you can only call it on an object of typeBessjy. See this for some details. For an example of how you would do this in your code, see this.Bessjy::j0()were a static member function then you can make your code work with some modifications. Here is a simplified example.