[英]C++: armadillo matrices inside struct
I am using the Armadillo library in C++, I work with a group of particles having each of them its own position and velocity in space. 我正在使用C ++中的Armadillo库,我使用一组粒子,每个粒子在空间中都有自己的位置和速度。 This is why I considered to create an array of Particle where Particle is defined as a 3x2 matrix (first column=3d position, second column=3d velocity).
这就是为什么我考虑创建一个“粒子”数组的情况,其中“粒子”被定义为3x2矩阵(第一列= 3d位置,第二列= 3d速度)。 I tried this:
我尝试了这个:
struct Particle{
arma::mat state(3,2);
};
but doesn't work, it tells me "expected a type specifier". 但不起作用,它告诉我“期望类型说明符”。 I simply want to initialize a 3x2 matrix (possibly with zeros) every time i create a Particle object.
每次创建粒子对象时,我只想初始化3x2矩阵(可能为零)。
I tried also this: 我也试过这个:
struct Particella {
arma::mat::fixed<3,2> state;
};
which works (even if i don't know how to initialize it) but I don't know why the first statement doesn't. 哪个有效(即使我不知道如何初始化它),但我不知道为什么第一条语句不起作用。
The first code is trying to call a constructor where you declare the variable, which afaik with parentheses is illegal. 第一个代码试图调用构造函数,在该构造函数中声明变量,带括号的afaik是非法的。 member initialization reference
成员初始化参考
With c++11 you could do 使用c ++ 11,您可以做到
struct Particle {
// There might be a better way to do this but armadillo's doc is currently down.
arma::mat state{arma::mat(3, 2)};
};
If not available you could try initializing the mat inside Particle's initializer list like this 如果不可用,您可以尝试像这样在Particle的初始化列表中初始化该垫
struct Particle {
Particle() : state(arma::mat(3, 2)) {}
private:
arma::mat state;
};
Or in a more C-like way.. 或更像C的方式
struct Particle {
arma::mat state;
};
Particle p;
p.state = arma::mat(3, 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.