0

sigc::mem_fun用来将作为类成员的处理程序函数连接到 GTKMM 事件。我sigc::mem_fun从声明处理函数的同一个类中调用。

// Interface.h

class Interface {
public:
    Interface(...);
// ...
private:
    Gtk::ComboBoxText *optionChoiceComboBoxText = nullptr; //Initialized in constructor
    Gtk::Switch optionControlSwitch;

    void optionChangedHandler();

    void switchHandler();
// ...
};

// Interface.cpp

Interface::Interface(...) {
    // ...
    optionChoiceComboBoxText->signal_changed().connect(sigc::mem_fun(*this, &Interface::optionChangedHandler));
    optionControlSwitch.signal_state_changed().connect(sigc::mem_fun(*this, &Interface::switchHandler)); // Error is here
}


void Interface::optionChangedHandler() {
    // Do something
}

void Interface::switchHandler() {
    // Do something
}

这是错误:

In file included from /usr/include/sigc++-2.0/sigc++/functors/slot.h:7:0,
                 from /usr/include/sigc++-2.0/sigc++/signal_base.h:27,
                 from /usr/include/sigc++-2.0/sigc++/signal.h:8,
                 from /usr/include/sigc++-2.0/sigc++/sigc++.h:86,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:51,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/sigc++-2.0/sigc++/adaptors/adaptor_trait.h: In instantiation of ‘typename sigc::adaptor_functor<T_functor>::deduce_result_type<T_arg1>::type sigc::adaptor_functor<T_functor>::operator()(T_arg1) const [with T_arg1 = const Gtk::StateType&; T_functor = sigc::bound_mem_functor0<void, Interface>; typename sigc::adaptor_functor<T_functor>::deduce_result_type<T_arg1>::type = void]’:
/usr/include/sigc++-2.0/sigc++/functors/slot.h:142:20:   required from ‘static T_return sigc::internal::slot_call1<T_functor, T_return, T_arg1>::call_it(sigc::internal::slot_rep*, sigc::type_trait_take_t<T_arg3>) [with T_functor = sigc::bound_mem_functor0<void, Interface>; T_return = void; T_arg1 = Gtk::StateType; sigc::type_trait_take_t<T_arg3> = const Gtk::StateType&]’
/usr/include/sigc++-2.0/sigc++/functors/slot.h:149:37:   required from ‘static void* (* sigc::internal::slot_call1<T_functor, T_return, T_arg1>::address())(void*) [with T_functor = sigc::bound_mem_functor0<void, Interface>; T_return = void; T_arg1 = Gtk::StateType; sigc::internal::hook = void* (*)(void*)]’
/usr/include/sigc++-2.0/sigc++/functors/slot.h:584:90:   required from ‘sigc::slot1<T_return, T_arg1>::slot1(const T_functor&) [with T_functor = sigc::bound_mem_functor0<void, Interface>; T_return = void; T_arg1 = Gtk::StateType]’
/usr/include/sigc++-2.0/sigc++/functors/slot.h:1731:26:   required from ‘sigc::slot<T_return, T_arg1, sigc::nil, sigc::nil, sigc::nil, sigc::nil, sigc::nil, sigc::nil>::slot(const T_functor&) [with T_functor = sigc::bound_mem_functor0<void, Interface>; T_return = void; T_arg1 = Gtk::StateType]’
/home/crefrod/RNS/src/GUI/Interface.cpp:38:103:   required from here
/usr/include/sigc++-2.0/sigc++/adaptors/adaptor_trait.h:89:30: error: no match for call to ‘(sigc::bound_mem_functor0<void, Interface>) (const Gtk::StateType&)’
     { return functor_(_A_arg1); }
                              ^
In file included from /usr/include/sigc++-2.0/sigc++/adaptors/adaptor_trait.h:9:0,
                 from /usr/include/sigc++-2.0/sigc++/functors/slot.h:7,
                 from /usr/include/sigc++-2.0/sigc++/signal_base.h:27,
                 from /usr/include/sigc++-2.0/sigc++/signal.h:8,
                 from /usr/include/sigc++-2.0/sigc++/sigc++.h:86,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:51,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/sigc++-2.0/sigc++/functors/mem_fun.h:1786:12: note: candidate: T_return sigc::bound_mem_functor0<T_return, T_obj>::operator()() const [with T_return = void; T_obj = Interface]
   T_return operator()() const
            ^
/usr/include/sigc++-2.0/sigc++/functors/mem_fun.h:1786:12: note:   candidate expects 0 arguments, 1 provided
In file included from /usr/include/sigc++-2.0/sigc++/functors/slot.h:7:0,
                 from /usr/include/sigc++-2.0/sigc++/signal_base.h:27,
                 from /usr/include/sigc++-2.0/sigc++/signal.h:8,
                 from /usr/include/sigc++-2.0/sigc++/sigc++.h:86,
                 from /usr/include/glibmm-2.4/glibmm/thread.h:51,
                 from /usr/include/glibmm-2.4/glibmm.h:87,
                 from /usr/include/giomm-2.4/giomm.h:23,
                 from /home/crefrod/RNS/src/GUI/Interface.cpp:5:
/usr/include/sigc++-2.0/sigc++/adaptors/adaptor_trait.h:89:30: error: return-statement with a value, in function returning 'void' [-fpermissive]
     { return functor_(_A_arg1); }
                              ^

另一件奇怪的事情是为什么编译器会在第二次sigc::mem_fun调用时抛出错误。是它是如何工作的,还是与第一次调用是在指针上完成,第二次调用是在对象本身上完成的事实有关?

4

1 回答 1

0

原因很明显:Gtk::Widget::signal_state_changed()接受并传递一个参数,但你尝试将它连接到一个零参数的槽。

从文档中引用

插槽原型: void on_my_state_changed(Gtk::StateType previous_state)

顺便说一句,该文档还指出该插槽已被弃用,您应该使用另一个插槽:

on_my_state_flags_changed(Gtk::StateFlags previous_state_flags)

无论如何,要修复这种错误:让您的处理函数采用正确数量和类型的参数。

于 2018-10-05T15:35:18.933 回答