0

I have 4 buttons to add and fill a Gtk::Box to a grid, instead of having 4 different functions for essentially the same operation I would like to pass a value to the function and use if statements to fill the box depending on which button is pressed.

Header file:

Gtk::Button tAdd, pAdd, dAdd, bAdd;
void card_add(int columnChoice);

Source file:

tAdd.signal_clicked().connect(sigc::mem_fun(*this, &tabs::card_add(1)));
pAdd.signal_clicked().connect(sigc::mem_fun(*this, &tabs::card_add(2)));
dAdd.signal_clicked().connect(sigc::mem_fun(*this, &tabs::card_add(3)));
bAdd.signal_clicked().connect(sigc::mem_fun(*this, &tabs::card_add(4)));

void tabs::card_add(int columnChoice) {
    std::cout << columnChoice << std::endl;
}

The error I receive:

error: lvalue required as unary ‘&’ operand
KJ465
  • 33
  • 5
  • You have a function call where a function pointer is expected: `sigc::mem_fun(*this, &tabs::card_add(1))`. If you want to pass additional arguments, you have to use `sigc::bind`. This binding often drove me crazy. (Meanwhile I'm using Qt5 where lambda functions can be used as signal handlers making all the annoying binding crap obsolete.) I believe it has to be: `tAdd.signal_clicked().connect(sigc::bind(sigc::mem_fun(*this, &tabs::card_add), 1));`. – Scheff's Cat Jul 10 '20 at 10:55
  • Out of curiosity, I googled a bit and found that the most recent versions of gtkmm/sigc should be able to use lambda functions as well. [SO: use lambda in connection with sigc library](https://stackoverflow.com/a/53537407/7478597) So, you may try as well `tAdd.signal_clicked().connect([this]() { card_add(1); });`. – Scheff's Cat Jul 10 '20 at 11:01
  • this worked great thank you, the documentation on sigc::bind as the example was quite specific so I just assumed it wasn't what I needed to use – KJ465 Jul 10 '20 at 14:51
  • The `sigc::bind` itself doesn't look so complicated. Even nested `sigc::bind`s seem to be quite manageable. The real horror starts when `sigc::bind`s and `sigc::hide`s have to be combined (to hide signal arguments from function call _and_ adding values which are required by the signal handler). I never could remember which has to be nested into which to make it working and ended up just with testing the possible alternatives until I got rid of the compiler errors. :-) – Scheff's Cat Jul 10 '20 at 14:58

0 Answers0