
Not sure why this was changed, though I've never used bind in this way (specifically, to bind to a reference of a member variable). Are you free to make changes to the code? This should work in either version: void AddUserAge( _sUserData& d ) { // <-- change this to a reference to the struct ++d.age; } int _tmain(int argc, _TCHAR* argv[]) { std::vector<_sUserData> vUserData; for( int i = 0; i < 10; ++i ) vUserData.push_back( _sUserData() ); std::for_each( vUserData.begin(), vUserData.end(), boost::bind( AddUserAge, _1 )); // <-- and remove the inner bind return 0; } Or you could just do this in 1.34.0 or higher: #include <boost/foreach.hpp> .... int _tmain(int argc, _TCHAR* argv[]) { std::vector<_sUserData> vUserData( 10 ); // vector of 10 _sUserData, default constructed BOOST_FOREACH( _sUserData& d, vUserData ) ++d.age; return 0; } Of course, all this is starting to look like a homework problem to me... 2008/6/3 风舞影天 <snwcwt@qq.com>:
I'm a newbie in boost library and sorry for my poor English.
I have a project now, it was created in Visual Studio.Net 2003 and the version of the boost library is 1.33.1, all thing is good. But, When I replaced the boost library with the new version 1.34.1 or 1.35.0, the project can't compiled correct. The error message is 'can't convert const sometype& to sometype&'. So, i force myself to read the source code of boost library and compare them betwwen version 1.33.1 and version 1.44.1.at last, I found the problem and written a test program. The following code is the test program:
#include "stdafx.h" #include <boost/bind.hpp> #include <vector> #include <algorithm>
struct _sUserData { int m_nUserId; int m_nUserAge; _sUserData() : m_nUserId( rand() ) , m_nUserAge( rand()%100+1 ) { } };
void AddUserAge( int& age ) { ++age; }
int _tmain(int argc, _TCHAR* argv[]) { std::vector<_sUserData> vUserData; for( int i = 0; i < 10; ++i ) vUserData.push_back( _sUserData() ); std::for_each( vUserData.begin(), vUserData.end(), boost::bind( AddUserAge, boost::bind( &_sUserData::m_nUserAge, _1 ) ) ); return 0; } If i change the function AddUserAge( int& age ) to AddUserAge( const int& age ), all things is good. but I need change this parameter.