Antares
Antares

Reputation: 13

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

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.

Upvotes: 0

Views: 364

Answers (1)

aram
aram

Reputation: 1444

The first code is trying to call a constructor where you declare the variable, which afaik with parentheses is illegal. member initialization reference

With c++11 you could do

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

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

Or in a more C-like way..

struct Particle {
    arma::mat state;
};

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

Upvotes: 0

Related Questions