Compile error using boost's tr1::bind

This code: #include <iostream> #include <algorithm> #include <boost/tr1/functional.hpp> using std::ostream; class Value { public: Value() : m_amount(0) {} void IncreaseBy(int amount) { m_amount += amount; } void WriteTo(ostream& stream) const { stream << m_amount; } private: int m_amount; }; ostream& operator <<(ostream& stream, const Value& value) { value.WriteTo(stream); return stream; } int main() { Value sum; int values[] = { 1, 2, 3, 4, 5 }; std::for_each(values, values + 5, std::tr1::bind(&Value::IncreaseBy, sum)); std::cout << "The value is " << sum << std::endl; return 0; } Produces compile error output like the following when compiled on Fedora 10 (Intel): In file included from /usr/include/boost/tr1/functional.hpp:8, from ValueSum.cpp:3: /usr/include/boost/tr1/detail/config.hpp:60:26: error: no include path in which to search for utility /usr/include/boost/mem_fn.hpp: In member function ‘R& boost::_mfi::dm<R, T>::operator()(T&) const [with R = void ()(int), T = Value]’: /usr/include/boost/bind.hpp:221: instantiated from ‘R boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = void (&)(int), F = boost::_mfi::dm<void ()(int), Value>, A = boost::_bi::list1<int&>, A1 = boost::_bi::value<Value>]’ /usr/include/boost/bind/bind_template.hpp:32: instantiated from ‘typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = int, R = void (&)(int), F = boost::_mfi::dm<void ()(int), Value>, L = boost::_bi::list1<boost::_bi::value<Value> >]’ /usr/lib/gcc/i386-redhat-linux/4.3.2/../../../../include/c++/4.3.2/bits/stl_algo.h:3791: instantiated from ‘_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = int*, _Funct = boost::_bi::bind_t<void (&)(int), boost::_mfi::dm<void ()(int), Value>, boost::_bi::list1<boost::_bi::value<Value> > >]’ ValueSum.cpp:31: instantiated from here /usr/include/boost/mem_fn.hpp:359: error: invalid use of non-static member function What is needed for the code to compile and run properly?

What is needed for the code to compile and run properly?
Which Boost version and is Boost installed in /usr/include or somewhere similar? Basically this is a known problem for some Boost versions when Boost is installed in your *system search path* - the issue is that the Boost headers are in a location that is searched *after* the libstdc++ headers and that breaks the header-forwarding mechanism used by Boost.TR1. The solution is probably to upgrade to the latest Boost release, or if all else fails, the SVN Trunk. HTH, John.

Tron Thomas: ...
int main() { Value sum; int values[] = { 1, 2, 3, 4, 5 }; std::for_each(values, values + 5, std::tr1::bind(&Value::IncreaseBy, sum)); std::cout << "The value is " << sum << std::endl; return 0; }
You need to use std::for_each(values, values + 5, std::tr1::bind(&Value::IncreaseBy, &sum, _1)); &sum (or, if you prefer, std::tr1::ref(sum)) is needed so that bind doesn't make a copy of sum. The _1 is needed to receive the number from values and pass it to IncreaseBy as the 'amount'.
participants (3)
-
John Maddock
-
Peter Dimov
-
Tron Thomas