c++ - 用模板替换标量失败

标签 c++ templates gcc eigen eigen3

我正在尝试编写一个函数,它从 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/

相关文章:

c++ - 对 C++ 中的基本递归感到困惑

c++ - 使用WIC将QImage编码为png?

templates - 如何在Odoo中继承没有ID的模板?

c++ - 在 C++ 中推断 lambda 的模板类型

c - exit() 的系统调用实现

c++ - 在Visual Studio 2019中处理异常

c++用2星替换空格

c++ - 模板特化 - clang 和 gcc 的不同结果

gcc - 对 `__ctype_b_loc' 等的 undefined reference

c - 使用 C 字符串查找和替换