I want to call a method of my class inside a lambda expression:
void my_class::my_method(my_obj& obj)
{
}
void my_class::test_lambda()
{
std::list<my_obj> my_list;
std::for_each(my_list.begin(), my_list.end(), [](my_obj& obj)
{
// Here I want to call my_method:
// my_method(obj);
});
}
How can I do?
You need to capture
this
, either explicitly or implicitly:You can't call a non-static method without an object to call it on.
Make a
my_class
object and capture a reference to it in the lambda...Or, if you meant the lambda was in a method of
my_class
then capturethis
. Or, if it's a static method then you can callmy_class::my_method(obj)
without capturing anything, like bames53 said below.