This forum has been archived. All content is frozen. Please use KDE Discuss instead.

std::accumulate fails to compile for std::vector<EigenObjec>

Tags: None
(comma "," separated)
hijokpayne
Registered Member
Posts
25
Karma
0
I have a simple code:
Code: Select all
std::vector<Eigen::Vector2i, Eigen::aligned_allocator<Eigen::Vector2i> > vec;
Eigen::Vector2i sum = std::accumulate(vec.begin(), vec.end(), Eigen::Vector2i::Zero());

which gives me following error. The default + operatuin, which I am asking for, is already defined by Eigen. Isn't it?
Code: Select all
/usr/include/c++/4.6/bits/stl_numeric.h:128:2: error: no match for ‘operator=’ in ‘__init = Eigen::MatrixBase<Derived>::operator+(const Eigen::MatrixBase<OtherDerived>&) const [with OtherDerived = Eigen::Matrix<int, 2, 1>, Derived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Matrix<int, 2, 1> >, typename Eigen::internal::traits<T>::Scalar = int]((*(const Eigen::MatrixBase<Eigen::Matrix<int, 2, 1> >*)__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Eigen::Matrix<int, 2, 1>*, _Container = std::vector<Eigen::Matrix<int, 2, 1>, Eigen::aligned_allocator_indirection<Eigen::Matrix<int, 2, 1> > >, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Eigen::Matrix<int, 2, 1>&]()))’
/usr/include/c++/4.6/bits/stl_numeric.h:128:2: note: candidate is:
/usr/local/include/eigen3/Eigen/src/Core/CwiseNullaryOp.h:49:7: note: Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Matrix<int, 2, 1> >& Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Matrix<int, 2, 1> >::operator=(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Matrix<int, 2, 1> >&)
/usr/local/include/eigen3/Eigen/src/Core/CwiseNullaryOp.h:49:7: note:   no known conversion for argument 1 from ‘const Eigen::CwiseBinaryOp<Eigen::internal::scalar_sum_op<int>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Matrix<int, 2, 1> >, const Eigen::Matrix<int, 2, 1> >’ to ‘const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, Eigen::Matrix<int, 2, 1> >&’
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I guess this is because the STL is not compatible with Expression templates. Anyway, a better way would be to use a Matrix<int,2,Dynamic> vec. and vec.rowwise().sum().
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
oh, the fix is actually simple: Vector2i::Zero() is not a Vector2i, so it has to be evaluated as follow:

Code: Select all
std::vector<Eigen::Vector2i, Eigen::aligned_allocator<Eigen::Vector2i> > vec;
Eigen::Vector2i sum = std::accumulate(vec.begin(), vec.end(), Eigen::Vector2i::Zero().eval());
hijokpayne
Registered Member
Posts
25
Karma
0
Thanks for the solution. I am also converteing my code to use Matrix<int,2,Dynamic> over stl containers since it offers more advantages.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]