Call a member function with member data using for_each

Dear, I would like to call a member function (which expects a link) for each object (say) of a vector that is a member of the same class, as shown in the following code:

#include <functional>  
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

struct Stuff {
  double x;
};

class Test {
public:
  void f1(Stuff & thing);
  void f2(void);
  vector<Stuff> things;
};

void Test::f1(Stuff & thing) {
  ; // do nothing
}

void Test::f2(void) {
  for_each(things.begin(), things.end(), f1);
}

int main(void)
{

  return 0;
}  

These codes give me a compiler error related to an unresolved overloaded function type. I also tried linking, but it seems that the link details in f1 are one problem. I know that I am missing something important here, so I take this opportunity to solve my problem and find out. I cannot install boost at the moment, but I would also like to know if boost can help solve this problem. Thanks in advance.

+3
1
  • , , f1, &Test::f1 ( : - f1 Test)
  • f1 : - this Test * const
  • , , , .

Boost.Bind :

std::for_each(things.begin(), things.end(), boost::bind(&Test::f1, this, _1));
+4

Source: https://habr.com/ru/post/1775490/


All Articles