1

I downloaded Eigen (3) library and started using it. I wrote a template function and declared a local variable of 'template type' inside the function. I am getting the following compilation error.


$ g++ EigenTest.cpp

EigenTest.cpp: In instantiation of ‘void myFunc(Eigen::MatrixBase<Derived>&) [with Type1 = Eigen::Matrix<double, -1, -1>]’:
EigenTest.cpp:24:10:   required from here
EigenTest.cpp:16:26: error: conversion from ‘Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, -1>, 1>::Scalar {aka double}’ to non-scalar type ‘Eigen::Matrix<double, -1, -1>’ requested
   Type1 tmp = matrix(0, 0);

"EigenTest.cpp" is given below.


#include "Eigen/Dense"

#include <iostream>

template<typename Type1>
void myFunc(Eigen::MatrixBase<Type1>& matrix)
{
int i=matrix.rows();
Type1 tmp = matrix(0, 0);           // getting compiler error here
std::cout<<"tmp is ->"<<tmp<<std::endl;
}

int main()
{
Eigen::MatrixXd m(2,2);
m.setConstant(100); 
myFunc(m);
return 0;
}

I also tried using 'typename Type1 tmp = matrix(0, 0);'
This also didn't work!

How to fix this? In normal C++ template programming (without Eigen), I can define a local variable inside a template function as 'Type1 tmp;"

5
  • What is Type1? And the program you show in the question is not the one that generated the errors. Please show the real program. Commented Oct 31, 2013 at 8:54
  • Sorry for missing out some part of the code in he initial post. Type1 is actually 'template typename'
    – Sooraj
    Commented Oct 31, 2013 at 9:01
  • Simply put though, it looks like the return from the call matrix(0, 0) (which I don't quite understand, but it is early) does not match the type Type1 - whatever that is, you haven't shown us what Type1 is defined as, presumably in some typedef somewhere. Commented Oct 31, 2013 at 9:02
  • I suggest you learn what a SSCCE is. Commented Oct 31, 2013 at 9:03
  • Guys, the code was missing <>, I now get the error as described by the OP.
    – rubenvb
    Commented Oct 31, 2013 at 9:07

3 Answers 3

3

In Eigen::MatrixBase<Type1>, Type1 is not a scalar type but the type of the actual expression. In your example it will be MatrixXd but if myFunc is called on, e.g., m.block(...), then Type1 will be a Block<...>. To obtain the scalar type, you can use Type1::Scalar:

template<typename Type1>
void myFunc(Eigen::MatrixBase<Type1>& matrix)
{
  typename Type1::Scalar Scalar;
  Scalar tmp = matrix(0, 0);
}

And if you need a matrix type that is similar to Type1, use Type1::PlainObject, e.g.:

typename Type1::PlainObject mat = 2 * matrix * matrix.transpose();
3
  • Thanks for the reply. As you suggested, I used Type1::Scalar and it is working now.
    – Sooraj
    Commented Nov 1, 2013 at 16:15
  • i think the bottom code is supposed to be Type1::PlainObject, not PlainObjectType Commented Apr 15, 2016 at 19:03
  • @ofloveandhate, you're right, I've updated the answer.
    – ggael
    Commented Apr 15, 2016 at 20:10
1

It looks like MatrixBase uses the "CRTP" (see here), the template argument is actually the type deriving from it. Thus in your use of the method myFunc(), Type1 is actually representing Eigen::MatrixXd, and I think that you think Type1 is a double. So, this line:

Type1 tmp = matrix(0, 0);

In the documnetation for this library (see here) the typedef for MatrixXd is a matrix of doubles, so I guess the return from matrix(0, 0) is a double, and as tmp is of Type1 which is Eigen::MatrixXd, the one will not go into the other.

Scanning the docummentation I think it MIGHT be better for your function to take a Matrix as an argument, that way the scalar type should be available. Something like this:

template<class T, int rows, int cols, int opts, int maxR, int maxC > 
void myFunc( Eigen::Matrix<T, rows, cols, opts, maxR, maxC>& matrix )
{
    T tmp = matrix(0, 0);
}

(Looks dreadful though!!! ;-) )

4
  • OK, error output is even more misleading than I thought. Can't you use this to get at the underlying data type?
    – rubenvb
    Commented Oct 31, 2013 at 9:41
  • Not completely misleading. In the error message Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, -1>, 1>::Scalar refers to double (in your case) and then the message continues that it can't turn that into a Eigen::Matrix<double, -1, -1>, which is Type1, the type that derived from MatrixBase and was also its template parameter. Simples! (Not!) As for the other solution, could well be - I don't know this library, I just quickly scanned the documentation for the source of the error. Commented Oct 31, 2013 at 9:55
  • This might be one of those cases where MSVC could just give the most useful error (as it usually lists all the types relevant to a template instantiation). Anyways, nice work finding the real issue :-).
    – rubenvb
    Commented Oct 31, 2013 at 13:11
  • Glad I could help. Gladder still for an upvote and a tick. ;-) Commented Oct 31, 2013 at 14:07
0

In your code, Type1 is deduced to be double (because Eigen::MatrixXd is defined that way).

You are then trying to do

Type1 tmp = matrix(0, 0);

And I'm afraid my Eigen knowledge isn't enough, so I ran it through Clang 3.3, and got this error:

test.cpp:9:7: error: no viable conversion from 'Scalar' (aka 'double') to
      'Eigen::Matrix<double, -1, -1, 0, -1, -1>'
Type1 tmp = matrix(0, 0);           // getting compiler error here
      ^     ~~~~~~~~~~~~
test.cpp:17:1: note: in instantiation of function template specialization
      'myFunc<Eigen::Matrix<double, -1, -1, 0, -1, -1> >' requested here
myFunc(m);
^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:210:5: note: candidate constructor not viable:
      no known conversion from 'Scalar' (aka 'double') to
      'internal::constructor_without_unaligned_array_assert' for 1st argument
    Matrix(internal::constructor_without_unaligned_array_assert)
    ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:284:25: note: candidate constructor not
      viable: no known conversion from 'Scalar' (aka 'double') to 'const
      Eigen::Matrix<double, -1, -1, 0, -1, -1> &' for 1st argument
    EIGEN_STRONG_INLINE Matrix(const Matrix& other)
                        ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:272:25: note: candidate template ignored:
      could not match 'MatrixBase<type-parameter-0-0>' against 'double'
    EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
                        ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:292:25: note: candidate template ignored:
      could not match 'ReturnByValue<type-parameter-0-0>' against 'double'
    EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
                        ^
/usr/include/eigen3/Eigen/src/Core/Matrix.h:303:25: note: candidate template ignored:
      could not match 'EigenBase<type-parameter-0-0>' against 'double'
    EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
                        ^
1 error generated.

which is telling me you cannot call matrix like that, with two 0's as arguments. It's also weird syntax because the MatrixBase class does not have an operator() which you seem to be trying to calling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.