C++:结构内的犰狳矩阵

C++: armadillo matrices inside struct

我在 C++ 中使用 Armadillo 库,我使用一组粒子,每个粒子在 space 中都有自己的位置和速度。这就是为什么我考虑创建一个 Particle 数组,其中 Particle 被定义为 3x2 矩阵(第一列 = 3d 位置,第二列 = 3d 速度)。我试过这个:

 struct Particle{
    arma::mat state(3,2);
};

但不起作用,它告诉我 "expected a type specifier"。 我只是想在每次创建粒子对象时初始化一个 3x2 矩阵(可能带有零)。

我也试过这个:

struct Particella {
    arma::mat::fixed<3,2> state;
};

哪个有效(即使我不知道如何初始化它)但我不知道为什么第一个语句没有。

第一个代码试图在您声明变量的位置调用构造函数,这在 afaik 中是非法的。 member initialization reference

使用 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)}; 
};

如果不可用,您可以尝试像这样在 Particle 的初始化列表中初始化垫子

struct Particle {
    Particle() : state(arma::mat(3, 2)) {}
private:
    arma::mat state;
};

或者更像 C 的方式..

struct Particle {
    arma::mat state;
};

Particle p;
p.state = arma::mat(3, 2);