Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to use this header file roots_multidim.h from http://numerical.recipes

in my code
and I am getting this compile error

Severity Code Description Project File Line Suppression State Error C2988 unrecognizable template declaration/definition

please help me resolve this issue
here is a part of that header file

<pre lang="C++">
#include <iostream>
 template <class T>
void lnsrch(VecDoub_I &xold, const Doub fold, VecDoub_I &g, VecDoub_IO &p,
VecDoub_O &x, Doub &f, const Doub stpmax, Bool &check, T &func) {
	const Doub ALF=1.0e-4, TOLX=numeric_limits<Doub>::epsilon();
	Doub a,alam,alam2=0.0,alamin,b,disc,f2=0.0;
	Doub rhs1,rhs2,slope=0.0,sum=0.0,temp,test,tmplam;
	Int i,n=xold.size();
	check=false;
	for (i=0;i<n;i++) sum += p[i]*p[i];
	sum=sqrt(sum);
	if (sum > stpmax)
		for (i=0;i<n;i++)
			p[i] *= stpmax/sum;
	for (i=0;i<n;i++)
		slope += g[i]*p[i];
	if (slope >= 0.0) throw("Roundoff problem in lnsrch.");
	test=0.0;
	for (i=0;i<n;i++) {
		temp=abs(p[i])/MAX(abs(xold[i]),1.0);
		if (temp > test) test=temp;
	}
	alamin=TOLX/test;
	alam=1.0;
	for (;;) {
		for (i=0;i<n;i++) x[i]=xold[i]+alam*p[i];
		f=func(x);
		if (alam < alamin) {
			for (i=0;i<n;i++) x[i]=xold[i];
			check=true;
			return;
		} else if (f <= fold+ALF*alam*slope) return;
		else {
			if (alam == 1.0)
				tmplam = -slope/(2.0*(f-fold-slope));
			else {
				rhs1=f-fold-alam*slope;
				rhs2=f2-fold-alam2*slope;
				a=(rhs1/(alam*alam)-rhs2/(alam2*alam2))/(alam-alam2);
				b=(-alam2*rhs1/(alam*alam)+alam*rhs2/(alam2*alam2))/(alam-alam2);
				if (a == 0.0) tmplam = -slope/(2.0*b);
				else {
					disc=b*b-3.0*a*slope;
					if (disc < 0.0) tmplam=0.5*alam;
					else if (b <= 0.0) tmplam=(-b+sqrt(disc))/(3.0*a);
					else tmplam=-slope/(b+sqrt(disc));
				}
				if (tmplam>0.5*alam)
					tmplam=0.5*alam;
			}
		}
		alam2=alam;
		f2 = f;
		alam=MAX(tmplam,0.1*alam);
	}
}


What I have tried:

I don't know how to redefine it such that it works
Posted
Updated 18-Apr-23 10:14am
v3
Comments
Richard MacCutchan 18-Apr-23 12:27pm    
That question is such an unreadably mess it is unlikely that anyone here can help you. Please use the Improve question link above, and reformat the code, using <pre> tags around it, so it is readable.
Sabry1905 18-Apr-23 13:21pm    
Thank you for the advise
Richard MacCutchan 18-Apr-23 15:20pm    
There are many undefined types in the above code so it is not clear which part is causing the problem. I suggest you go back to the website where you got this code and ask the person who wrote it.

I would guess it is because the type for the template is not defined but, as Richard mentioned, there are so many undefined types in there that is difficult to know for sure. Personally, I see little reason to define standard variable types so I avoid doing that.

Anyway, to continue with my guessing, here is what I think your missing function declaration might look like :
C++
typedef double & (* func)( VecDoub_O &x );
based on what the types of the data I see in your code.

If this is the case, I see little benefit to this being a template function and I really don't see why it is. If you were to change the type of data to float you would have a ton of errors and warnings and it would not compile so what other types do you anticipate using?

If I were you, I would define exactly what the type is for func that you will be using and I would drop the template aspect of the function so it is just a plain function that takes a pointer to a function as one of its arguments. Also, you need to be careful about returning a reference to insure the variable is valid when the function returns. For a simple data type you do not need to return a reference since it can live on the stack with no ill-effects.
 
Share this answer
 
Searching for "numerical.recipes" and "roots_multidim.h" yields a github source.
https://github.com/blackstonep/Numerical-Recipes
In the file "nr3.h" all you need is declared, so i think you should include "nr3.h"
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900