KcFnMi
KcFnMi

Reputation: 6171

Eigen::Map unamed?

Down here I tried to write random values to a using Eigen function:

double *a = (double*)malloc(N*N*sizeof(double));

Map<Matrix<double, N, N, RowMajor> >m(a);
m = MatrixXd::Random(N,N);

Is it possible to do the last part in just one line (without creating m)? I was imagining something like

Map<Matrix<double, N, N, RowMajor> >(a) = MatrixXd::Random(N,N);

But got

main.cpp:44:42: error: redefinition of 'a' with a different type: 'Map<Matrix<double, N, N, RowMajor> >' vs 'double *'
    Map<Matrix<double, N, N, RowMajor> >(a) = MatrixXd::Random(N,N);
                                         ^
main.cpp:42:13: note: previous definition is here
    double *a = (double*)malloc(N*N*sizeof(double));
            ^
1 error generated.

Upvotes: 1

Views: 41

Answers (1)

chtz
chtz

Reputation: 18827

C++ will ignore the () around a in a construct like

Type (a) = expr;

You can either write

( Type(a) ) = expr;

or with C++11:

Type{a} = expr;

For this case you can also use the static Map member function:

Matrix<double, N, N, RowMajor>::Map(a) = MatrixXd::Random(N,N);

or

Matrix<double, N, N, RowMajor>::Map(a).setRandom(); // size is specified by type

Upvotes: 1

Related Questions