dimanche 26 mars 2017

Calling a class method from within a lambda

I am new to C++ lambdas, but thought they are quite straight-forward (know the concept from Python for example). However, the following does not work for me:

A custom class HungerGui contains as a member a pointer to class HungerGames. That class HungerGames has a public method Simulate(). I want to call that method from the class HungerGui, from within a lambda:

class HungerGui
{
   // Some class stuff ...

    m_SimBtn.events().click([this]
    {
        m_HungerGames->Simulate();
    });
}

This compiles fine, but when I run the code the following exception is thrown at the first line of (within) Simulate():

this is nullptr

However, when I remove the this from the lambda (m_SimBtn.events().click([]), the following error occurrs at compilation:

the usage of 'HungerGui::m_HungerGames' requires the compiler to capture 'this' but the current default capture mode does not allow it

Any ideas what I am missing here?

Aucun commentaire:

Enregistrer un commentaire