【发布时间】:2017-04-10 08:15:13
【问题描述】:
我想使用 Eigen 的张量类。 我已经安装了 Eigen 3.3.3 并确保我的项目选择了正确的版本(而不是旧的 ubuntu 包)
cout<<EIGEN_WORLD_VERSION<<"."<<EIGEN_MAJOR_VERSION<<"."<<EIGEN_MINOR_VERSION<<endl;
// prints 3.3.3
此代码有效:
Tensor<double, 3> t(3,3,3);
t.setConstant(1);
但是这段代码失败了:
t*=5;
错误信息抱怨int类型:
[ 50%] Building CXX object CMakeFiles/sandbox.dir/src/SandBox/sandbox.cpp.o
In file included from /opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/Tensor:103:0,
from /home/lars/programming/fsd_cpp/src/SandBox/sandbox.cpp:3:
/opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h: In instantiation of ‘Derived& Eigen::TensorBase<Derived, AccessLevel>::operator*=(const OtherDerived&) [with OtherDerived = int; Derived = Eigen::Tensor<double, 3>; int AccessLevel = 1]’:
/home/lars/programming/fsd_cpp/src/SandBox/sandbox.cpp:30:6: required from here
/opt/eigen333/include/eigen3/unsupported/Eigen/CXX11/src/Tensor/TensorBase.h:876:36: error: request for member ‘derived’ in ‘other’, which is of non-class type ‘const int’
return derived() = derived() * other.derived();
我尝试过t*=5.;,但这只会将错误消息更改为which is of non-class type ‘const double’
当我将代码更改为:
t=t*5;
错误信息变得相当长:https://pastebin.com/T6kvLaZ9
最终版本:
t=t*5.;
令人惊讶的是,这行得通。我不明白为什么t*=5.; 会产生错误。
【问题讨论】:
标签: c++ templates eigen eigen3 tensor