Reputation: 33
I am trying to write code that needs to define a matrix size based upon an input. A stripped down version of the code that shows the issue is:
#include <iostream>
#include "eigen/Eigen/Dense"
#include <cmath>
using namespace Eigen;
void matrixname( const int numbRow, const int numbcol);
int main()
{
const int numbRow=5;
const int numbCol=3;
matrixname(numbRow,numbCol);
return 0;
}
void matrixname( const int numbRow, const int numbCol)
{
Matrix<double,numbRow,numbCol> y;
}
When attempting to compile the code, the following error is returned:
/main.cpp:20:15: error: non-type template argument is not a constant expression
The build breaks in the last line of trying to define y.
Is there any way I can modify the declaration or passing of the variables in order to be able to define the size of the matrix in this way?
Upvotes: 3
Views: 1637
Reputation:
As per the documentation, if you don't know the size of a matrix at compile time you need to use the matrix size template parameters as Eigen::Dynamic
.
So you may have to modify your function as follows:
void matrixname( const int numbRow, const int numbCol)
{
Matrix<double,Eigen::Dynamic,Eigen::Dynamic> y1(numbRow, numbCol);
// Eigen also provides a typedef for this type
MatrixXd y2(numbRow, numbCol);
}
Upvotes: 1