The problem is compiler errors with the code snippet below.
Here's a very simple program to fill a list with random integers and increment each element. I use a std::for_each call to a functor to increment each member of my collection and all compiled. Focus on the for_each in main(). Next I simply alter the for_each to call List::increment rather than the functor Increment (notice the commented out code in main). For this task I use mem_fun_ref since I am not dealing with a collection of pointers but of classes. Also I don't think I need bind1st or bind2nd.
Here's my compiler errors:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function _Function std::for_each(_InputIterator, _InputIterator, _Function) [with _InputIterator = std::_List_iterator, _Function = std::mem_fun1_ref_t]': blahblah.cpp:53: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:158: error: no match for call to (std::mem_fun1_ref_t) (unsigned int&)'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:826: note: candidates are: void std::mem_fun1_ref_t::operator()(_Tp&, _Arg) const [with _Tp = List, _Arg = unsigned int&]
#include <list>
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>
static const unsigned int LIST_SIZE = 25;
typedef std::list<unsigned int> List_t;
class List
{
public:
List() {};
~List() {};
void push_back(unsigned int in) {m_list.push_back(in);};
List_t::iterator begin( ) {m_list.begin();}
List_t::iterator end( ) {m_list.end();};
void print_list ( ) const
{
std::cout << std::endl;
for (List_t::const_iterator iter = m_list.begin(); iter != m_list.end(); ++iter)
{
std::cout << *iter << ' ';
}
std::cout << std::endl << std::endl;
}
void increment( unsigned int & input)
{
++input;
}
private:
List_t m_list;
};
struct Increment{
void operator ()(unsigned int &input)
{
++input;
}
};
int main()
{
List l;
for (unsigned int i= 0; i <= LIST_SIZE; i++)
{
unsigned int x = rand() % 100;
l.push_back(x);
}
l.print_list(); // pre incremented
//for_each(l.begin(),l.end(),Increment());
for_each(l.begin(),l.end(),std::mem_fun_ref(&List::increment));
l.print_list(); // incremented
return 1;
}