0

I'm trying to write a function that takes an Eigen::Matrix from either type double or float. This function works fine for floats:

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;
}

However, as soon as I make the "float" a 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.block<3,3>(0,0) = T.block<3,3>(0,0).transpose();
   return result;
}

I get this error with gcc 4.9.1 on linux:

.../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();

What could be the problem here?

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105

1 Answers1

3

Once the function is template some calls are dependent of template and so you have to add some template keyword, try:

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;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Awesome, that works! Although I'm not sure I understand what exactly happens, and what "template block" does... could you explain in detail or give me some pointer to an explanation? – Jan Rüegg Oct 02 '14 at 13:01
  • 1
    You may look at [where-and-why-do-i-have-to-put-the-template-and-typename-keywords](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Jarod42 Oct 02 '14 at 13:04