我正在尝试编写一个函数,它从 double 或 float 类型中获取 Eigen::Matrix。此函数适用于 float :
Eigen::Matrix<float, 4, 4> foo(const Eigen::Matrix<float, 4, 4> &T)
{
Eigen::Matrix<float, 4, 4> result;
result.block<3,3>(0,0) = T.block<3,3>(0,0).transpose();
return result;
}
但是,一旦我将“ float ”设为模板:
template <typename Scalar>
inline Eigen::Matrix<Scalar, 4, 4> foo(const Eigen::Matrix<Scalar, 4, 4> &T)
{
Eigen::Matrix<Scalar, 4, 4> result;
result.block<3,3>(0,0) = T.block<3,3>(0,0).transpose();
return result;
}
我在 linux 上使用 gcc 4.9.1 时遇到这个错误:
.../utils.hpp: In function 'Eigen::Matrix core::math::foo(const Eigen::Matrix&)': .../utils.hpp:77:47: error: request for member 'transpose' in '(0, 0)', which is of non-class type 'int' result.block<3,3>(0,0) = T.block<3,3>(0,0).transpose();
这可能是什么问题?
最佳答案
一旦函数是模板,一些调用依赖于模板,所以你必须添加一些 template
关键字,试试:
template <typename Scalar>
inline Eigen::Matrix<Scalar, 4, 4> foo(const Eigen::Matrix<Scalar, 4, 4> &T)
{
Eigen::Matrix<Scalar, 4, 4> result;
result.template block<3,3>(0,0) = T.template block<3,3>(0,0).transpose();
return result;
}
关于c++ - 用模板替换标量失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156730/