我需要一个具有Eigen :: Ref变量的类作为静态成员,该成员将通过init静态方法进行初始化。像这样的东西:
class CostFunction {
public:
static Eigen::Ref<Eigen::VectorXd> data;
static void init(const Eigen::Ref<Eigen::VectorXd>& d) {
data = d;
}
CostFunction() {}
};
int main() {
Eigen::VectorXd data = Eigen::VectorXd::Random(30);
CostFunction cf;
cf.init(data);
return 0;
}
这不会编译。我收到一个看起来像这样的错误:
/var/tmp/doNotRemove/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/Ref.h: In instantiation of ‘Eigen::RefBase<Derived>& Eigen::RefBase<Derived>::operator=(const Eigen::RefBase<Derived>&) [with Derived = Eigen::Ref<const Eigen::Matrix<double, -1, 1> >]’:
/var/tmp/doNotRemove/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/Ref.h:229:77: required from here
/var/tmp/doNotRemove/builds/fit3dceres/RHEL6_AMD64_GCC484_OPT/include/eigen3/Eigen/src/Core/util/Macros.h:608:26: error: use of deleted function ‘Eigen::MapBase<Eigen::Ref<const Eigen::Matrix<double, -1, 1> >, 0>& Eigen::MapBase<Eigen::Ref<const Eigen::Matrix<double, -1, 1> >, 0>::operator=(const Eigen::MapBase<Eigen::Ref<const Eigen::Matrix<double, -1, 1> >, 0>&)’
Base::operator=(other); \
一般而言,看起来Eigen :: Ref无法分配给另一个Eigen :: Ref。有谁知道为什么有此限制,以及是否有办法将Ref存储为类的静态成员变量?
PS:我使用的是Eigen :: Ref,因为此处的文档:https : //eigen.tuxfamily.org/dox-devel/classEigen_1_1Ref.html听起来像是正确的选择,它是实现函数时要使用的通用类型它应该适用于大多数本征类型(例如,在我的情况下,在VectorXd和Map上)。
在您的情况下,您最好使用VectorXd
,否则必须确保VectorXd
传递给init的对象永远不会被破坏。
在Ref
此处使用a的唯一原因将是允许data
使用例如a的列进行初始化Matrix
而没有任何副本。
最后,如果您想重新分配ARef
来引用另一个缓冲区,请使用new放置来重新调用Ref的构造函数。不要忘记先调用析构函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句