[英]How to correctly use a matrix (Eigen library) in my own class in C++?
I really tried to solve this problem and I have read a lot of QAs here on Stackoverflow but somehow nothing really helped. 我确实试图解决此问题,并且在Stackoverflow上阅读了很多QA,但实际上并没有任何帮助。
I am trying to implement a Class with its own matrices and vectors from the Eigen library. 我正在尝试使用Eigen库中的矩阵和向量来实现Class。 I am using Code::Blocks with GNU GCC Compiler. 我在GNU GCC编译器中使用Code :: Blocks。
class MYCLASS{
private:
VectorXd x;
MatrixXd A;
public:
MYCLASS(double, double, double, double);
double get_matval();
};
MYCLASS::MYCLASS(double deltaT, double q_var, double r1_var, double r2_var){
x_m(0)=q_var;
x_m(1)=r1_var;
x_m(2)=r2_var;
x_m(3)=0.0;
A(0,0)= deltaT;
A(0,1)= 0.0;
A(1,0)= 0.0;
A(1,1)= 0.0;
}
double MYCLASS::get_matval(){
return A(0,0);
}
MYCLASS myobject(10, 0.5, 0.1, 0.75);
==> Compilation is good, but when it runs, then the program somehow crashes with the following exact description (keep in mind I have other matrices and vectors): ==>编译是好的,但是当它运行时,程序将以以下确切描述崩溃(请记住,我还有其他矩阵和向量):
Assertion failed: index >= 0 && index < size(), file F:....../Eigen/src/Core/DenseCoeffsBase.h, line 425 断言失败:索引> = 0 &&索引<size(),文件F:...... / Eigen / src / Core / DenseCoeffsBase.h,第425行
This application has requested the Runtime to terminate it in an unusual way. 该应用程序已请求运行时以一种异常方式终止它。 Please contact... 请联系...
Process returned 3 (0x3) execution time: 2.131 s 进程返回3(0x3)执行时间:2.131 s
MYCLASS::MYCLASS(double deltaT, double q_var, double r1_var, double r2_var){
VectorXd x(4);
MatrixXd A(2,2);
x_m(0)=q_var;
x_m(1)=r1_var;
...and so on...
==> then when constructing the object ==> no error ==>然后在构造对象时==>没有错误
==> but when I then want to access A(0,0) by using the function get_matval, like this: ==>但是当我随后想要使用函数get_matval访问A(0,0)时,如下所示:
MYCLASS myobject(10, 0.5, 0.1, 0.75);
double myvar = myobject.get_matval();
...it crashes again with the same error message. ...它再次崩溃,并显示相同的错误消息。
Could you guys please help? 你们能帮忙吗?
Even though you found a solution already, here is how to properly initialize (a limited number of) Eigen matrices: 即使您已经找到解决方案,也可以通过以下方法正确初始化(有限数量的)本征矩阵:
MYCLASS::MYCLASS(double deltaT, double q_var, double r1_var, double r2_var)
: x(4), A(2,2) { // use initializer list to construct x and A
x << q_var, r1_var, r2_var, 0.0;
A << deltaT, 0.0, 0.0, 0.0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.