我使用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
调用时抛出错误的原因。是它的工作原理还是与第一次调用是在指针上进行,第二次调用是在对象本身上完成的事实有什么关系?
答案 0 :(得分:0)
原因很明显:Gtk::Widget::signal_state_changed()接受并传递一个参数,但是您尝试将其连接到零参数的插槽。
引用文档
广告位原型: 无效on_my_state_changed(Gtk :: StateType previous_state)
顺便说一句,该文档还指出该插槽已被弃用,您应该改用另一个插槽:
on_my_state_flags_changed(Gtk::StateFlags previous_state_flags)
无论如何,要纠正这种错误:请使您的处理函数使用正确数量和类型的参数。