template instantiation issue with complex data types

95 Views Asked by At

I have the type of parameters as

typedef double DP;
typedef const NRVec<DP> Vec_I_DP; //NRVec is a parametrized template class.
typedef NRVec<DP> Vec_DP, Vec_O_DP, Vec_IO_DP;

xa -> Vec_I_DP(30)
ya -> Vec_I_DP(30)
yp1 -> DP(30)
ypn -> DP(30)
y2 -> Vec_O_DP(30)

My function call is

NR::spline(xa,          ya,          yp1,          ypn,         y2);

And the function declaration is

void spline(Vec_I_DP &x, Vec_I_DP &y, const DP yp1, const DP ypn,Vec_O_DP &y2);

Can somebody tell me, why I get the following error.

1>Hiwi.obj : error LNK2019: unresolved external symbol "void __cdecl NR::spline(class NRVec<double> const &,class NRVec<double> const &,double,double,class NRVec<double> &)" (?spline@NR@@YAXABV?$NRVec@N@@0NNAAV2@@Z) referenced in function "void __cdecl smooth_Line(double *,int *,int,int,double *,double *,int,int)" (?smooth_disp_Line@@YAXPANPAHHH00HH@Z)
1

There are 1 best solutions below

1
CroCo On

It seems you are using NR2. The newest version is NR3 which defines spline in a class called Spline_interp. You should use NR3. For this matter, I will show you an example for using the aforementioned class. Let's say you have five values for sin(x) and you can to interpolate these values and acquire the interpolated values, say sin(x=.25). In this example, we know true value, therefore, we can compare the result with the interpolated value. In the following code, I got the interpolated value plus the error.

#include <iostream>
#include <iomanip>
#include "nr3.h"
#include "interp_1d.h" 

int main() {
    VecDoub x(5), y(5);

    //x = 0:0.3:1
    //y = sin(x)

    x[0] = 0.0;
    x[1] = 0.3;
    x[2] = 0.6;
    x[3] = 0.9;

    y[0] = 0.0;
    y[1] = 0.2955;
    y[2] = 0.5646;
    y[3] = 0.7833;

    /**************************************************
        3.3 Cubic Spline Interpolation
        page(120)
    ***************************************************/

    Spline_interp myfunc3(x,y);
    std::cout << "Cubic Spline Interpolation: " << std::endl;
    std::cout << "interp value: " << myfunc3.interp(.25)  << "\n" 
              << "  true value: " << sin(0.25)            << "\n" 
              << "       error: " << ( ( sin(0.25) - myfunc3.interp(.25) )/sin(0.25) )*100.0  << "%" << std::endl;


    std::cout << std::setw(25) << std::cout.fill('=') << std::endl; 


    return 0;
} 

For compilation in Windows from the command prompt,

cl /EHsc main.cpp /Fetest.exe /I path\to\NR\code

The result is

Cubic Spline Interpolation:
interp value: 0.247187
  true value: 0.247404
       error: 0.0876794%
========================

The error is very small.