My question is related to numerical recipes.
I have a global function which computes the vector of functions to be minimized
VecDoub vecfunc(VecDoub_I x) {
// code is here
}
In a class function run, I tried to call the Numerical Recipes function newt, which reads my function vecfunc as shown,
class A {
void run() {
VecDoub_IO pt;
pt.resize(2);
pt[0] = 0.5;
pt[1] = 0.5;
bool check = false;
newt<VecDoub>(pt, check, &vecfunc);
}
}
Function newt is declared as
template <class T>
void newt(VecDoub_IO &x, Bool &check, T &vecfunc)
Why do I get the following compiler error?
error C2664: 'newt' : cannot convert parameter 3 from 'VecDoub (__cdecl *)(VecDoub_I)' to 'VecDoub &'
In calling
newtyou explicitly specify thatTisVecDoub(You specifiednewt<VecDoub>) but you pass address of a function to it, so compiler can’t convert your function toVecDoub&. If you want aVecDoub&innewtthen callvectfuncand store it in a temporary variable and then pass that variable to the function(since innewtlast parameter is a reference toT) but if you really need a function innewtthen why you writenewt<VecDoub>(pt, check, &vecfunc)while you can writenewt(pt, check, &vecfunc)and let C++ deduce the type for you? and beside that in order to receive functions innewtdo not get them by reference, instead get them by value, sonewtshall be declared as:Since functions are usually small objects or pointers this will work and do not decrease performance