Search code examples
c++classeigeneigen3

How to deal with this eigen error of static assertion failed?


I'm using vscode to write my cpp code. It use the Eigen package. And I met a strange error:


error: static assertion failed: YOU_CALLED_A_FIXED_SIZE_METHOD_ON_A_DYNAMIC_SIZE_MATRIX_OR_VECTOR

214 | EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)

  |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I don't know what does it mean.So I ask someone for help. And my code is:

#include<iostream>
#include<vector>
#include<Eigen/Dense>
using namespace std;
class relax{
    public:
        relax(int mn,int mm,double mxmin,double mxmax,double mymin,double mymax,double merror);
        ~relax();
    private:
        int n;
        int m;
        double xmin;
        double xmax;
        double ymin;
        double ymax;
        double error;
        Eigen::MatrixXd U;
        Eigen::MatrixXd U1;
        double rho(int i,int j);
};
relax::relax(int mm,int mn,double mxmin,double mxmax,double mymin,double mymax,double merror){
    xmin=mxmin;
    xmax=mxmax;
    ymin=mymin;
    ymax=mymax;
    m=mm;
    n=mm;
    error=merror;
    U.resize(m+2,n+2);
    U=Eigen::MatrixXd::Zero();
}

relax::~relax(){

}

double relax::rho(int m,int n){
    return 0;
}
int main(){
    return 0;
}

Solution

  • Eigen::MatrixXd::Zero(rows, cols) can be used to create a matrix initialized to 0. E.g. U=Eigen::MatrixXd::Zero(m+2,n+2);

    If you already have defined a matrix and want to set it to zero, use the setZero function: U.setZero();.