title: Call a member function using for_each
body: <p>This was my original code</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;string&gt;
  #include &lt;list&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  
  class testing
  {
  public:
      int value;
      testing(int v)
      {
          value = v;
      }
  
      int getval()
      {
          return(value);
      }
  
  };
  
  void func(testing&amp; ob)
  {
      printf("The value is %d\n", ob.value);
  }
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      std::list&lt;testing&gt; testvar[3];
  
      testing t1(0);
      testing t2(1);
      testing t3(3);
  
      testvar[0].push_back(t1);
      testvar[0].push_back(t2);
      testvar[0].push_back(t3);
  
      std::for_each(testvar[0].begin(), testvar[0].end(), func);
  
      printf("Reached End");
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>I modified it to make func a member function and got weird compile errors, I searched online and someone had told use bind1st, bind2nd</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;string&gt;
  #include &lt;list&gt;
  #include &lt;algorithm&gt;
  using namespace std;
  
  class testing
  {
  public:
  int value;
  testing(int v)
  {
      value = v;
  }
  
  int getval()
  {
      return(value);
  }
  
  };
  
  class testing2
  {
  public:
      std::list&lt;testing&gt; testvar[3];
  
      void func(testing&amp; ob)
      {
          printf("The value is %d\n", ob.value);
      }
  
      void driver()
      {
          std::for_each(testvar[0].begin(), testvar[0].end(), func);
      }
  
  };
  
  
  
  int _tmain(int argc, _TCHAR* argv[])
  {
      testing2 testob;
  
      testob.driver();
  
  
      printf("Reached End");
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>So I modified the driver function to this </p>
  
  <pre><code>    void driver()
      {
  std::for_each(testvar[0].begin(), testvar[0].end(),     std::bind1st(std::mem_fun(&amp;testing2::func), this));
      }
  </code></pre>
  
  <p>I still get some weird compile erros, could someone please expain why we need to call a member function is such weird way..? and how does bind1st help..? </p>