-2

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;
}
Learning Lin
  • 121
  • 1
  • 7
  • 1
    I'm no expert in Eigen (in fact, I've never used Eigen), but if I had to hazard a guess I would say it's because you used a function that only works on fixed-sized vectors or matrices with a dynamically-sized vector or matrix – Human-Compiler Oct 19 '20 at 12:16
  • Emm,but as I know,the MatrixXd::Zero() is designed to deal with a dynamic matrix.@Human-Compiler – Learning Lin Oct 19 '20 at 12:18
  • I don't know why. I can't google to find its answer.. – Learning Lin Oct 19 '20 at 12:19
  • 2
    Are you trying to set the matrix to zero? i.e. `U.setZero();`? – JHBonarius Oct 19 '20 at 12:21
  • emm,MatrixXd::Zero() only can be used in initialized.@Human-Compiler – Learning Lin Oct 19 '20 at 12:24
  • Yeah,it's this problem's answer.@JHBonarius – Learning Lin Oct 19 '20 at 12:25
  • 2
    Use the member initializer list instead of resizing the matrix: `relax::relax(int mm, int mn, double mxmin, double mxmax, double mymin, double mymax, double merror) : n(mm), m(mm), xmin(mxmin), xmax(mxmax), ymin(mymin), ymax(mymax), error(merror), U(m + 2, n + 2) {}` – Ted Lyngmo Oct 19 '20 at 12:32

1 Answers1

4

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

JHBonarius
  • 10,824
  • 3
  • 22
  • 41