dimanche 14 mai 2023

Binding arbitrary function in wxwidgets

I want to create a number of composite objects, each containing a scrolled list and some buttons (for my example, I've reduced the code to creating just one of the objects with just one button). All of these objects will be very similar, except for the layout, size, and the functions called by the buttons (i.e. for each object, the callback for the same button can be different).

So, in the constructor for these composite objects, I want to receive the functions to bind to the buttons (again, in my example, it will be one button).

I have an example version that works (without the Bind function being passed), and an example that won't compile when I try to add the code passing in the function to Bind.

Here's the compiler error:

stack2.cpp: In constructor ‘MainPanel::MainPanel(wxFrame*)’:
stack2.cpp:106:94: error: no matching function for call to ‘MyAddRemoveList::MyAddRemoveList(MainPanel*, const char [12], const char [11], int, int, int, bool, void (MyAddRemoveList::*)(wxCommandEvent&))’
  106 | xLB_MULTIPLE | wxLB_HSCROLL | wxLB_NEEDED_SB, false, &MyAddRemoveList::OnAddToList);
                                                                           1,68          Top
tack2.cpp:61:1: note: candidate: ‘MyAddRemoveList::MyAddRemoveList(wxWindow*, const char*, const char*, int, int, long int, bool, func)’
   61 | MyAddRemoveList::MyAddRemoveList(wxWindow *panel, const char *display_name,
      | ^~~~~~~~~~~~~~~
stack2.cpp:62:79: note:   no known conversion for argument 8 from ‘void (MyAddRemoveList::*)(wxCommandEvent&)’ to ‘func’ {aka ‘void (*)(wxCommandEvent&)’}
   62 |       const char *table_name, int w, int h, long style, bool do_extract, func fpt) :
      |                                                                          ~~~~~^~~
stack2.cpp:41:7: note: candidate: ‘MyAddRemoveList::MyAddRemoveList(const MyAddRemoveList&)’
   41 | class MyAddRemoveList : public wxBoxSizer {
      |       ^~~~~~~~~~~~~~~
stack2.cpp:41:7: note:   candidate expects 1 argument, 8 provided
stack2.cpp:41:7: note: candidate: ‘MyAddRemoveList::MyAddRemoveList(MyAddRemoveList&&)’
stack2.cpp:41:7: note:   candidate expects 1 argument, 8 provided
make: *** [stack2.make:94: stack2.o] Error 1

I don't understand why the compiler can't cast void (*)(wxCommandEvent&) to void (MyAddRemoveList::*)(wxCommandEvent&). This is my problem.

Notes:

  • I'm new to stackoverflow, so please have mercy.
  • I'm completely new to both c++ and wxwidgets, so please have mercy.
  • I have pruned the code down to a minimal version (one that "works" without the Bind, and one with Bind that won't compile). But again, they're each about 100 lines and I don't know if that's too long to include in a question here.
  • POP OS 22.04
  • wxwidgets 3.0

Any help/suggestions would be much appreciated. Thanks in advance...

Aucun commentaire:

Enregistrer un commentaire