user3294195 user3294195 - 1 year ago 169
C++ Question

Eigen - Map a const array to a dynamic vector

I need to define a function that takes a

C array and maps it into an
map. The following code gives me an error:

double data[10] = {0.0};
typedef Eigen::Map<Eigen::VectorXd> MapVec;

MapVec fun(const double* data) {
MapVec vec(data, n);
return vec;

If I remove
from the function definition the code works fine. But is it possible to retain the
without any errors?


Answer Source

If the Map's parameter is a non-const type (e.Eigen::VectorXd) then it assumes that it can modify the raw buffer (in your case *data). As the function expects a const qualified buffer, you have to tell the map that it's const. Define your typedef as

typedef Eigen::Map<const Eigen::VectorXd> MapVec;

and it should work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download