复制赋值运算符将rhs作为非常量
DeterministicStateType &operator=(DeterministicStateType &a) {
当调用代码中的源代码为const时,这既不是必需的,也不会编译。只需通过添加常量来修复它:
DeterministicStateType &operator=(DeterministicStateType const& a) {
State &operator=(State const&a) {
mMap = a.mMap;
return *this;
}
State &operator=(State const&a) = default;
等等,为什么我的错误信息还在这里?
integrate_adaptive
. 临时约束仅限于
const&
,从不
&
DeterministicStateType
在调用之前,通过引用传递它,而不是临时
DeterministicStateType(x0)
:
int main() {
DeterministicStateType x0 { { {"A", 1.0}, {"B", 1.0} } };
typedef runge_kutta_dopri5<DeterministicStateType, double, DeterministicStateType, double, vector_space_algebra>
stepper_type;
integrate_adaptive(make_dense_output(1e-6, 1e-6, stepper_type()), derivative, x0, 0.0,
300.0, 0.00001);
}
简化代码
在这个简化版本中,我使用了一个名称空间
mMap
Live On Wandbox
#include <boost/numeric/odeint.hpp>
#include <boost/operators.hpp>
#include <iostream>
#include <map>
namespace Deterministic {
class State : boost::additive1<State,
boost::additive2<State, double,
boost::multiplicative2<State, double> > >
{
public:
using Map = std::map<std::string, double>;
State(Map const& map) : mMap(map) {}
State() = default;
State(const State &p) = default;
State &operator=(State const&a) = default;
State &operator+=(const State &p) {
for (auto& p : p.mMap) mMap[p.first] += p.second;
return *this;
}
State &operator+=(double a) {
for (auto& p : mMap)
p.second += a;
return *this;
}
State &operator*=(double f) {
for (auto& p : mMap) mMap[p.first] *= f;
return *this;
}
friend State abs(const State &p) {
using std::abs;
auto map = p.mMap;
for(auto& e : map)
e.second = abs(e.second);
return map;
}
friend State operator/(const State &p1, const State &p2) {
auto map = p1.mMap;
for(auto& e : map)
e.second /= p2.mMap.at(e.first);
return map;
}
friend double vector_space_norm_inf_impl(State const& p) {
double max = 0;
using std::abs;
for (auto& el : p.mMap)
max = std::max(abs(el.second), max);
return max;
}
size_t size() const { return mMap.size(); }
void resize(State const& other) {
for (auto& el : other.mMap)
mMap[el.first] += 0; // inserts if non-existent
}
private:
Map mMap;
};
}
using DeterministicStateType = Deterministic::State;
namespace boost { namespace numeric { namespace odeint {
template <> struct vector_space_norm_inf<DeterministicStateType> {
typedef double result_type;
double operator()(const DeterministicStateType &p) const { return vector_space_norm_inf_impl(p); }
};
template <> struct is_resizeable<DeterministicStateType> {
typedef boost::true_type type;
const static bool value = type::value;
};
template <> struct same_size_impl<DeterministicStateType, DeterministicStateType> {
static bool same_size(const DeterministicStateType &v1, const DeterministicStateType &v2) {
return v1.size() == v2.size();
}
};
template <> struct resize_impl<DeterministicStateType, DeterministicStateType> {
static void resize(DeterministicStateType &v1, const DeterministicStateType &v2) {
v1.resize(v2);
}
};
} } }
void derivative(const DeterministicStateType, DeterministicStateType &, const double) {}
using namespace boost::numeric::odeint;
int main() {
DeterministicStateType x0 { { {"A", 1.0}, {"B", 1.0} } };
typedef runge_kutta_dopri5<DeterministicStateType, double, DeterministicStateType, double, vector_space_algebra>
stepper_type;
integrate_adaptive(make_dense_output(1e-6, 1e-6, stepper_type()), derivative, x0, 0.0,
300.0, 0.00001);
}
除了坏掉的编译器