【问题标题】:template instantiation issue with complex data types复杂数据类型的模板实例化问题
【发布时间】:2015-11-09 15:55:37
【问题描述】:

我的参数类型为

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)

【问题讨论】:

标签: c++ templates numerical-recipes


【解决方案1】:

您似乎正在使用 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%
========================

误差很小。

【讨论】:

  • 如果您真的想使用NR2,请通知我,以便我可以发布有关此问题的答案,但是,我强烈建议您使用NR3
猜你喜欢
  • 2014-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多