Search code examples
eventslambdawxwidgets

wxWidgets checkbox event lambda


Trying to use lambda to define event handlers for controls of the wxWidgets framework:

wxCheckBox*c=new wxCheckBox(panel,wxID_ANY,"...",wxDefaultPosition,wxDefaultSize);
c->Bind(wxEVT_CHECKBOX,[](wxCommandEvent& event){
    wxMessageBox(wxT("This is the message."),wxT("This is the title"),wxICON_INFORMATION);
});

I get these error messages below, complaining about the signature? Or do I need to pass compiler flags to GCC to ensure compatibility with lambdas?

E:\bootsi\New folder\test.cpp:61:3: error: no matching function for cal
l to 'wxCheckBox::Bind(const wxEventTypeTag<wxCommandEvent>&, MyFrame::MyFrame(c
onst wxString&, const wxPoint&, const wxSize&)::__lambda0)'
  });
   ^
E:\bootsi\New folder\test.cpp:61:3: note: candidates are:
In file included from D:/build/wxWidgets-3.0.1/include/wx/wx.h:24:0,
                 from E:\bootsi\New folder\test.cpp:5:
D:/build/wxWidgets-3.0.1/include/wx/event.h:3524:10: note: template<class EventT
ag, class EventArg> void wxEvtHandler::Bind(const EventTag&, void (*)(EventArg&)
, int, int, wxObject*)
     void Bind(const EventTag& eventType,
          ^
D:/build/wxWidgets-3.0.1/include/wx/event.h:3524:10: note:   template argument d
eduction/substitution failed:
E:\bootsi\New folder\test.cpp:61:3: note:   mismatched types 'void (*)(
EventArg&)' and 'MyFrame::MyFrame(const wxString&, const wxPoint&, const wxSize&
)::__lambda0'
  });
   ^
In file included from D:/build/wxWidgets-3.0.1/include/wx/wx.h:24:0,
                 from E:\bootsi\New folder\test.cpp:5:
D:/build/wxWidgets-3.0.1/include/wx/event.h:3550:10: note: template<class EventT
ag, class Functor> void wxEvtHandler::Bind(const EventTag&, const Functor&, int,
 int, wxObject*)
     void Bind(const EventTag& eventType,
          ^
D:/build/wxWidgets-3.0.1/include/wx/event.h:3550:10: note:   template argument d
eduction/substitution failed:
E:\bootsi\New folder\test.cpp: In substitution of 'template<class Event
Tag, class Functor> void wxEvtHandler::Bind(const EventTag&, const Functor&, int
, int, wxObject*) [with EventTag = wxEventTypeTag<wxCommandEvent>; Functor = MyF
rame::MyFrame(const wxString&, const wxPoint&, const wxSize&)::__lambda0]':
E:\bootsi\New folder\test.cpp:61:3:   required from here
E:\bootsi\New folder\test.cpp:61:3: error: template argument for 'templ
ate<class EventTag, class Functor> void wxEvtHandler::Bind(const EventTag&, cons
t Functor&, int, int, wxObject*)' uses local type 'MyFrame::MyFrame(const wxStri
ng&, const wxPoint&, const wxSize&)::__lambda0'
  });
   ^
E:\bootsi\New folder\test.cpp:61:3: error:   trying to instantiate 'tem
plate<class EventTag, class Functor> void wxEvtHandler::Bind(const EventTag&, co
nst Functor&, int, int, wxObject*)'
In file included from D:/build/wxWidgets-3.0.1/include/wx/wx.h:24:0,
                 from E:\bootsi\New folder\test.cpp:5:
D:/build/wxWidgets-3.0.1/include/wx/event.h:3579:10: note: template<class EventT
ag, class Class, class EventArg, class EventHandler> void wxEvtHandler::Bind(con
st EventTag&, void (Class::*)(EventArg&), EventHandler*, int, int, wxObject*)
     void Bind(const EventTag &eventType,
          ^
D:/build/wxWidgets-3.0.1/include/wx/event.h:3579:10: note:   template argument d
eduction/substitution failed:
E:\bootsi\New folder\test.cpp:61:3: note:   mismatched types 'void (Cla
ss::*)(EventArg&)' and 'MyFrame::MyFrame(const wxString&, const wxPoint&, const
wxSize&)::__lambda0'
  });
   ^
CMakeFiles\test.dir\build.make:54: recipe for target 'CMakeFiles/FreshS
ettings.dir/test.cpp.obj' failed
mingw32-make[3]: *** [CMakeFiles/test.dir/test.cpp.obj] Error
1
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/test.dir/all' fa
iled
mingw32-make[2]: *** [CMakeFiles/test.dir/all] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/test.dir/rule' f
ailed
mingw32-make[1]: *** [CMakeFiles/test.dir/rule] Error 2
Makefile:109: recipe for target 'test' failed
mingw32-make: *** [test] Error 2
Press any key to continue . . .

Solution

  • You need to build (both the library and your own code) with -std=c++11 g++ option for lambdas to work. You also need a version of g++ with C++11 support, of course, so at least 4.7.