转换特征矩阵类型时,错误:预期主表达式位于“float”之前

Bow*_*Wen 3 c++ templates casting eigen

template <typename T>\n      bool operator()(const T* parameters, T* residuals) const\n    {\n        Eigen::Matrix<T, 3, 1> pose(parameters[0],parameters[1],parameters[2]);\n        Eigen::Vector3f pose1 = pose.cast<float>();\n        Eigen::Affine2f transform = occ->getTransformForState(pose1);  // transform: rotation->translation\n\n\n        Eigen::Vector3f tmp1 = occ->interpMapValueWithDerivatives(  transform * currPoint);\n\n        Eigen::Matrix<T, 3, 1> transformedPointData(tmp1.cast<T>());  /// {M,dM/dx,dM/dy}\n\n        T funVal = T(1) - transformedPointData[0];\n\n        residuals[0] = funVal;\n\n        return true;\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

我有一个像上面这样的模板成员函数。在编译期间,它报告

\n
\n

错误:\xe2\x80\x98float\xe2\x80\x99 之前需要主表达式

\n
         Eigen::Vector3f pose1 = pose.cast<float>();\n
Run Code Online (Sandbox Code Playgroud)\n
\n

我必须转换为“float”类型,以使其与"getTransformForState"函数的输入和输出一致。

\n

我与 Eigen Library 提供的其他示例进行了比较,但没有发现任何错误。

\n

任何想法都受到高度赞赏!

\n

- - - - - - - - - - 更新 - - - - - - - - - - - -

\n

通过改变为pose.template cast<float>()

\n

错误改为:

\n
/usr/include/eigen3/Eigen/src/Core/MathFunctions.h: In instantiation of \xe2\x80\x98static NewType Eigen::internal::cast_impl<OldType, NewType>::run(const OldType&) [with OldType = ceres::Jet<double, 3>; NewType = float]\xe2\x80\x99:\n/usr/include/eigen3/Eigen/src/Core/MathFunctions.h:328:44:   required from \xe2\x80\x98NewType Eigen::internal::cast(const OldType&) [with OldType = ceres::Jet<double, 3>; NewType = float]\xe2\x80\x99\n/usr/include/eigen3/Eigen/src/Core/Functors.h:351:104:   required from \xe2\x80\x98const NewType Eigen::internal::scalar_cast_op<Scalar, NewType>::operator()(const Scalar&) const [with Scalar = ceres::Jet<double, 3>; NewType = float]\xe2\x80\x99\n/usr/include/eigen3/Eigen/src/Core/CwiseUnaryOp.h:114:75:   required from \xe2\x80\x98const Scalar Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::coeff(Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::Index) const [with UnaryOp = Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>; XprType = const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1>; Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::Scalar = float; Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::Index = long int]\xe2\x80\x99\n/usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h:495:33:   required from \xe2\x80\x98void Eigen::DenseCoeffsBase<Derived, 1>::copyCoeff(Eigen::DenseCoeffsBase<Derived, 1>::Index, const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>, const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1> >; Derived = Eigen::Matrix<float, 3, 1>; Eigen::DenseCoeffsBase<Derived, 1>::Index = long int]\xe2\x80\x99\n/usr/include/eigen3/Eigen/src/Core/Assign.h:180:5:   required from \xe2\x80\x98static void Eigen::internal::assign_LinearTraversal_CompleteUnrolling<Derived1, Derived2, Index, Stop>::run(Derived1&, const Derived2&) [with Derived1 = Eigen::Matrix<float, 3, 1>; Derived2 = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>, const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1> >; int Index = 0; int Stop = 3]\xe2\x80\x99\n/usr/include/eigen3/Eigen/src/Core/Assign.h:314:21:   [ skipping 5 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]\n/usr/include/eigen3/Eigen/src/Core/Matrix.h:281:31:   required from \xe2\x80\x98Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>, const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1> >; _Scalar = float; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]\xe2\x80\x99\n
Run Code Online (Sandbox Code Playgroud)\n

mel*_*ene 8

错误消息意味着编译器不知道这pose.cast是一个模板。班级成员有以下三种基本选择.foo

  1. 一个值(例如数据成员或enum值)
  2. 类型名称(例如用 定义的名称typedef
  3. 上述之一的模板。

在你的情况下,pose是类型Eigen::Matrix<T, 3, 1>。编译器还不知道是什么Eigen::Matrix<T, 3, 1>样子,因为它取决于是什么T(有人可能Eigen::Matrix对不同类型进行了不同的专门化)。

因此,当您访问未知类的成员(如pose.cast)时,编译器假定它是选项#1(一个值)。这导致它被解析pose.cast <为小于比较的开始。下一个标记 ( float) 会触发错误,因为编译器需要另一个值,而不是类型名称。

解决方法是明确告诉编译器这.cast是一个模板:

Eigen::Vector3f pose1 = pose.template cast<float>();
Run Code Online (Sandbox Code Playgroud)

(案例 #2 的修复方法是使用typename关键字强制解释为类型名称。)