-4

我尝试用 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)
4

1 回答 1

0

错误消息告诉您该怎么做:

 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;

但强烈建议不要这样做:为什么“使用命名空间标准”被认为是不好的做法?

所以我建议:

namespace bmath = boost::math;

然后,boost::math::sph_bessel(1, x)你可以写:bmath::sph_bessel(1, x)

于 2018-09-23T21:25:29.843 回答