在转换Eigen矩阵类型时,错误:在'float'之前的预期primary-expression

问题描述 投票:1回答:1
template <typename T>
      bool operator()(const T* parameters, T* residuals) const
    {
        Eigen::Matrix<T, 3, 1> pose(parameters[0],parameters[1],parameters[2]);
        Eigen::Vector3f pose1 = pose.cast<float>();
        Eigen::Affine2f transform = occ->getTransformForState(pose1);  // transform: rotation->translation


        Eigen::Vector3f tmp1 = occ->interpMapValueWithDerivatives(  transform * currPoint);

        Eigen::Matrix<T, 3, 1> transformedPointData(tmp1.cast<T>());  /// {M,dM/dx,dM/dy}

        T funVal = T(1) - transformedPointData[0];

        residuals[0] = funVal;

        return true;
    }

我有一个模板成员函数,如上所述。在编译期间,它会报告

错误:'float'之前的预期primary-expression

         Eigen::Vector3f pose1 = pose.cast<float>();

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

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

任何想法都非常感谢!

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

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

错误更改为:

/usr/include/eigen3/Eigen/src/Core/MathFunctions.h: In instantiation of ‘static NewType Eigen::internal::cast_impl<OldType, NewType>::run(const OldType&) [with OldType = ceres::Jet<double, 3>; NewType = float]’:
/usr/include/eigen3/Eigen/src/Core/MathFunctions.h:328:44:   required from ‘NewType Eigen::internal::cast(const OldType&) [with OldType = ceres::Jet<double, 3>; NewType = float]’
/usr/include/eigen3/Eigen/src/Core/Functors.h:351:104:   required from ‘const NewType Eigen::internal::scalar_cast_op<Scalar, NewType>::operator()(const Scalar&) const [with Scalar = ceres::Jet<double, 3>; NewType = float]’
/usr/include/eigen3/Eigen/src/Core/CwiseUnaryOp.h:114:75:   required from ‘const 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]’
/usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h:495:33:   required from ‘void 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]’
/usr/include/eigen3/Eigen/src/Core/Assign.h:180:5:   required from ‘static 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]’
/usr/include/eigen3/Eigen/src/Core/Assign.h:314:21:   [ skipping 5 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/eigen3/Eigen/src/Core/Matrix.h:281:31:   required from ‘Eigen::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]’
c++ templates casting eigen
1个回答
2
投票

错误消息表示编译器不知道pose.cast是模板。类成员.foo可以有三种基本选项:

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

在你的情况下,poseEigen::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>();

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

© www.soinside.com 2019 - 2024. All rights reserved.