gpt4 book ai didi

c++ - 使用 odeint 时 Boost lib 错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:29:09 25 4
gpt4 key购买 nike

我在名为 C++ 的文件夹中下载了 odeint-v2。我创建了一个名为 HARMONIC.cpp 的新 cpp 文件。

#include <iostream>
#include <vector>
#include <boost/numeric/odeint.hpp>

using namespace boost::numeric::odeint;

std::vector<double> state_type;
double gam = 0.15;

void harmonic_oscillator( const state_type &x , state_type &dxdt , const double )
{
dxdt[0] = x[1];
dxdt[1] = -x[0] - gam*x[1];
}

int main(int, char**)
{
using namespace std;
using namespace boost::numeric::odeint;

state_type x(2);
x[0] = 1.0;
x[1] = 0.0;


size_t steps = integrate( harmonic_oscillator, x , 0.0 , 10.0 , 0.1 );
}

在ubuntu中编译时g++ HARMONIC.cpp -o har.输出

错误如下`

HARMONIC.cpp:4:36: fatal error :boost/numeric/odeint.hpp: 没有这样的文件或目录编译终止。

但是我在同一个文件夹中下载了所有 odeint-v2。

请帮帮我

最佳答案

这里有许多不同的问题。

  1. 您似乎对图书馆的功能以及从哪里获得图书馆感到困惑。到目前为止,最简单的方法是通过 sudo apt-get install libboost-all-dev 安装 Ubuntu boost 包。

  2. 您复制的示例缺少关键的 typedef;否则 state_type 未定义。这已得到修复。

  3. 我在末尾添加了一个简单的“完成”语句。

  4. 有了它,一切就都正常了。

演示:

/tmp$ g++ -o boost_ode boost_ode.cpp 
/tmp$ ./boost_ode
Done.
/tmp$

修复后的代码如下。

/tmp$ cat boost_ode.cpp 
#include <iostream>
#include <vector>
#include <boost/numeric/odeint.hpp>

using namespace boost::numeric::odeint;

typedef std::vector<double> state_type;
double gam = 0.15;

void harmonic_oscillator( const state_type &x , state_type &dxdt , const double )
{
dxdt[0] = x[1];
dxdt[1] = -x[0] - gam*x[1];
}

int main(int, char**)
{
using namespace std;
using namespace boost::numeric::odeint;

state_type x(2);
x[0] = 1.0;
x[1] = 0.0;


size_t steps = integrate( harmonic_oscillator, x , 0.0 , 10.0 , 0.1 );

std::cout << "Done." << std::endl;
}
/tmp$

关于c++ - 使用 odeint 时 Boost lib 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com