【问题标题】:wxWidgets checkbox event lambdawxWidgets 复选框事件 lambda
【发布时间】:2014-10-22 10:25:39
【问题描述】:

尝试使用 lambda 为 wxWidgets 框架的控件定义事件处理程序:

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);
});

我在下面收到这些错误消息,抱怨签名?还是我需要将编译器标志传递给 GCC 以确保与 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 . . .

【问题讨论】:

    标签: events lambda wxwidgets


    【解决方案1】:

    您需要使用-std=c++11 g++ 选项构建(两个库和您自己的代码)才能使 lambda 工作。当然,您还需要支持 C++11 的 g++ 版本,因此至少需要 4.7。

    【讨论】:

    • 在 wxwidgets 的情况下,我可以在 makefiles/options 的哪个位置为整个源代码树全局设置该标志?
    • 像往常一样,通过设置CXXFLAGS 变量(在configure 或make 命令行上,取决于您使用的内容)。
    • 我已经在 config.gcc 中更改了它,但 wx 不会重建(目标不需要做任何事情),因为它已经构建了,即使我指定了不同的 CFG 标志。如何以这种方式强制单独构建? D:\build\wxWidgets-3.0.1\build\msw&gt;mingw32-make makefile.gcc BUILD=release CFG=c++ 11
    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    相关资源
    最近更新 更多