Domanda

Hi i'm trying to add two std vectors with boost.range but i get a bunch of errors.

this works:

  std::transform(a.begin(),a.end(),b.begin(),a.begin(),std::plus<double>());

this doesn't:

  boost::transform(a,b,a,std::plus<double>());

with an error:

In file included from /usr/local/include/boost/range/algorithm.hpp:80:0,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.h:15,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:1:
/usr/local/include/boost/range/algorithm/transform.hpp: In function ‘OutputIterator boost::range_detail::transform_impl(SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator2, SinglePassTraversalReadableIterator2, OutputIterator, BinaryFunction) [with SinglePassTraversalReadableIterator1 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, SinglePassTraversalReadableIterator2 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, OutputIterator = std::vector<double>, BinaryFunction = std::plus<double>]’:
/usr/local/include/boost/range/algorithm/transform.hpp:90:33:   instantiated from ‘OutputIterator boost::range::transform(const SinglePassRange1&, const SinglePassRange2&, OutputIterator, BinaryOperation) [with SinglePassRange1 = std::vector<double>, SinglePassRange2 = std::vector<double>, OutputIterator = std::vector<double>, BinaryOperation = std::plus<double>]’
/home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:9:45:   instantiated from here
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: error: no match for ‘operator*’ in ‘*out’
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: note: candidates are:
/usr/include/c++/4.6/complex:399:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const std::complex<_Tp>&)
/usr/include/c++/4.6/complex:390:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const _Tp&)
/usr/include/c++/4.6/complex:381:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const std::complex<_Tp>&)
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: error: no match for ‘operator++’ in ‘++out’
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: note: candidate is:
/usr/local/include/boost/iterator/iterator_facade.hpp:722:3: note: template<class I, class V, class TC, class R, class D> typename boost::detail::postfix_increment_result<I, V, R, TC>::type boost::operator++(boost::iterator_facade<Derived, V, TC, R, D>&, int)

and neither this:

  std::vector<double> c;
  boost::transform(a,b,c,std::plus<double>());

with an error:

In file included from /usr/local/include/boost/range/algorithm.hpp:80:0,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.h:15,
                 from /home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:1:
/usr/local/include/boost/range/algorithm/transform.hpp: In function ‘OutputIterator boost::range_detail::transform_impl(SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator1, SinglePassTraversalReadableIterator2, SinglePassTraversalReadableIterator2, OutputIterator, BinaryFunction) [with SinglePassTraversalReadableIterator1 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, SinglePassTraversalReadableIterator2 = __gnu_cxx::__normal_iterator<const double*, std::vector<double> >, OutputIterator = std::vector<double>, BinaryFunction = std::plus<double>]’:
/usr/local/include/boost/range/algorithm/transform.hpp:90:33:   instantiated from ‘OutputIterator boost::range::transform(const SinglePassRange1&, const SinglePassRange2&, OutputIterator, BinaryOperation) [with SinglePassRange1 = std::vector<double>, SinglePassRange2 = std::vector<double>, OutputIterator = std::vector<double>, BinaryOperation = std::plus<double>]’
/home/kirill/Dropbox/work/projects/doing/quasiclass/dev/source/simple_pcet.cc:8:45:   instantiated from here
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: error: no match for ‘operator*’ in ‘*out’
/usr/local/include/boost/range/algorithm/transform.hpp:64:17: note: candidates are:
/usr/include/c++/4.6/complex:399:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const _Tp&, const std::complex<_Tp>&)
/usr/include/c++/4.6/complex:390:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const _Tp&)
/usr/include/c++/4.6/complex:381:5: note: template<class _Tp> std::complex<_Tp> std::operator*(const std::complex<_Tp>&, const std::complex<_Tp>&)
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: error: no match for ‘operator++’ in ‘++out’
/usr/local/include/boost/range/algorithm/transform.hpp:65:17: note: candidate is:
/usr/local/include/boost/iterator/iterator_facade.hpp:722:3: note: template<class I, class V, class TC, class R, class D> typename boost::detail::postfix_increment_result<I, V, R, TC>::type boost::operator++(boost::iterator_facade<Derived, V, TC, R, D>&, int)
È stato utile?

Soluzione

Your problem is that the third parameter to boost::transform is not a range, but rather an (output) iterator that is used to write the results into.

The following code compiles for me:

boost::transform ( a, b, a.begin(), std::plus<double>());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top