【问题标题】:how to use boost for bessel function in C++如何在 C++ 中将 boost 用于贝塞尔函数
【发布时间】:2019-02-27 05:25:34
【问题描述】:

我尝试用 C++ 编写球面贝塞尔函数并使用 #include <boost/math/special_functions/bessel.hpp> 和 sph_bessel(v,x) 在我的代码中,但发生错误说这没有在这个范围内声明。我用 g++ test.cpp 编译。请帮助我。

#include <cmath>
#include <iostream>
#include <boost/math/special_functions/bessel.hpp> 
using namespace std;
int main()
{
    // spot check for n == 1
    double x = 1.2345;
    cout << "j_1(" << x << ") = " << sph_bessel(1, x) << '\n';
}

编译代码:

g++ test.cpp

并给出这个错误:

 error: ‘sph_bessel’ was not declared in this scope
 cout << "j_1(" << x << ") = " << sph_bessel(1, x) << '\n';
 a.cpp:9:38: note: suggested alternative:
 In file included from a.cpp:3:0:
 /usr/include/boost/math/special_functions/bessel.hpp:544:79: note:            ‘boost::math::sph_bessel’
 ename detail::bessel_traits<T, T, policies::policy<> >::result_type     sph_bessel(unsigned v, T x)

【问题讨论】:

  • 您可能错过了为sph_bessel(v,x) 指定必要的命名空间限定符。
  • 请粘贴您的 cpp 文件的简单版本,编译时会显示此错误。还请粘贴此简单版本的编译器生成的完整错误消息。总之,请创建一个minimal reproducible example

标签: c++ boost bessel-functions


【解决方案1】:

错误信息告诉你该怎么做:

 a.cpp:9:38: note: suggested alternative:
             ‘boost::math::sph_bessel’

所以代码应该是:

 cout << "j_1(" << x << ") = " << boost::math::sph_bessel(1, x) << '\n';

或者你可以添加:

using namespace boost::math;

但强烈建议不要这样做:Why is “using namespace std” considered bad practice?

所以我建议:

namespace bmath = boost::math;

那么你可以写成:bmath::sph_bessel(1, x),而不是boost::math::sph_bessel(1, x)。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多