复杂数据类型的模板实例化问题



我的参数类型为

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)

我的函数调用是

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

函数声明是

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

有人能告诉我,为什么我会出现以下错误吗。

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)

您似乎在使用NR2。最新版本是NR3,它在一个名为Spline_interp的类中定义样条曲线。你应该使用NR3。对于这个问题,我将向您展示一个使用前面提到的类的示例。假设sin(x)有五个值,可以对这些值进行插值并获得插值,比如sin(x=.25)。在这个例子中,我们知道真实值,因此,我们可以将结果与插值进行比较。在下面的代码中,我得到了插值加上误差。

#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;
} 

对于在Windows中从命令提示符进行编译,

cl /EHsc main.cpp /Fetest.exe /I pathtoNRcode

结果是

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

误差很小。

最新更新