Reputation: 2893
I want class Test
to have a Eigen::Matrix that maps to Test::_a.
This means whenever I change the std::vector, the Eigen::Matrix would also immediately reflect the change because both the vector and the matrix uses the same chunk of memory to hold data.
This works:
#include <Eigen/Dense>
#include <vector>
int main() {
std::vector<int> a;
a.resize(10);
typedef Eigen::Matrix<int, 1, Eigen::Dynamic> mat_type;
Eigen::Map< mat_type > a_eigen(a.data(), a.size());
}
But this doesn't work....
#include <Eigen/Dense>
#include <vector>
class Test {
public:
Test(int size) {
_a.resize(size);
_a_eigen(_a.data(), size );
}
std::vector<int> _a;
Eigen::Map< Eigen::Matrix<int, 1, Eigen::Dynamic> > _a_eigen;
};
int main() {
Test t(10);
}
Below also doesn't work because cout prints nothing
#include <Eigen/Dense>
#include <vector>
#include <iostream>
class Test {
public:
typedef Eigen::Matrix<int, 1, Eigen::Dynamic> mat_type;
Test(int size) {
_a.resize(size);
Eigen::Map<mat_type>(_a_eigen) = Eigen::Map<mat_type>(_a.data(), size );
for (int i = 0; i < size; ++i) _a[i] = i;
}
std::vector<int> _a;
mat_type _a_eigen;
};
int main() {
Test t(10);
std::cout << t._a_eigen;
}
Upvotes: 2
Views: 1069
Reputation: 18817
You need to initialize the Map in the initializer-list of your class' constructor:
#include <Eigen/Core>
#include <vector>
class Test {
public:
Test(int size) : _a(size), _a_eigen(_a.data(), size) { }
std::vector<int> _a;
Eigen::Map< Eigen::Matrix<int, 1, Eigen::Dynamic> > _a_eigen;
};
Upvotes: 4