
4 Jun
2009
4 Jun
'09
2:10 p.m.
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'.