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

时间:2015-11-09 15:55:38

标签: c++ templates numerical-recipes

我的参数类型为

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)

1 个答案:

答案 0 :(得分:0)

看来你正在使用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 path\to\NR\code

结果是

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

错误非常小。