【问题标题】:Trying to bind a simple button to an OnClick function in SFGUI尝试将一个简单的按钮绑定到 SFGUI 中的 OnClick 函数
【发布时间】:2017-03-15 14:44:13
【问题描述】:

所以,我刚刚开始在我的 SFML 项目中使用 SFGUI,我很难理解一切是如何工作的。我一直在阅读 hello world 教程以及库本身在其文件夹中提供的一些示例。

我现在正在尝试创建一个简单的按钮,当它检测到左键单击时会执行某些操作,但我什至无法让它正常工作。错误窗口显示button 变量未在我的OnClick 方法中定义,如果我尝试使用std::bind 的参数发送button,我会得到典型的“没有匹配函数...”。

在该库的大多数示例中,通过使用 this 作为函数名旁边的第二个参数来解决此问题,但这似乎对我不起作用。

这应该怎么做?

我的代码:

void pressEnter(){
    button->SetLabel("Tocado"); // FIRST ERROR IN THIS LINE
}

int main(){

    sfg::SFGUI sfgui;

    sf::VideoMode video_mode( 800, 600 );
    sf::RenderWindow rwindow( video_mode, "In-joked - Menu Principal");
    sf::Event event;

    auto button = sfg::Button::Create("Status");
    button->GetSignal(sfg::Button::OnLeftClick).Connect(
            std::bind(&pressEnter, button)); //SECOND ERROR IN THIS LINE

    ...
}

NetBeans 第一行显示的错误: “无法解析标识符“按钮”。“按钮”未在此范围内声明”

对于第二行:

"在包含的文件中

错误:没有匹配函数调用‘sfg::Signal::Connect(std::_Bind_helper&>::type)’ std::bind(&pressEnter, button));"

运行控制台输出完全错误:

main.cpp: In function ‘void pressEnter()’:
main.cpp:7:5: error: ‘button’ was not declared in this scope
     button->SetLabel("Tocado");
     ^
In file included from /usr/include/c++/5/memory:79:0,
                 from /usr/local/include/SFGUI/Signal.hpp:6,
                 from /usr/local/include/SFGUI/Object.hpp:4,
                 from /usr/local/include/SFGUI/Adjustment.hpp:3,
                 from /usr/local/include/SFGUI/Widgets.hpp:6,
                 from main.cpp:2:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_check_arity<void (*)(), std::shared_ptr<sfg::Button>&>’:
/usr/include/c++/5/functional:1439:12:   required from ‘struct std::_Bind_helper<false, void (*)(), std::shared_ptr<sfg::Button>&>’
/usr/include/c++/5/functional:1462:5:   required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (*)(); _BoundArgs = {std::shared_ptr<sfg::Button>&}]’
main.cpp:20:42:   required from here
/usr/include/c++/5/functional:1410:7: error: static assertion failed: Wrong number of arguments for function
       static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
       ^
main.cpp: In function ‘int main()’:
main.cpp:20:43: error: no matching function for call to ‘sfg::Signal::Connect(std::_Bind_helper<false, void (*)(), std::shared_ptr<sfg::Button>&>::type)’
             std::bind(&pressEnter, button));
                                           ^
In file included from /usr/local/include/SFGUI/Object.hpp:4:0,
                 from /usr/local/include/SFGUI/Adjustment.hpp:3,
                 from /usr/local/include/SFGUI/Widgets.hpp:6,
                 from main.cpp:2:
/usr/local/include/SFGUI/Signal.hpp:40:16: note: candidate: unsigned int sfg::Signal::Connect(std::function<void()>)
   unsigned int Connect( std::function<void()> delegate );
                ^
/usr/local/include/SFGUI/Signal.hpp:40:16: note:   no known conversion for argument 1 from ‘std::_Bind_helper<false, void (*)(), std::shared_ptr<sfg::Button>&>::type {aka std::_Bind<void (*(std::shared_ptr<sfg::Button>))()>}’ to ‘std::function<void()>’
nbproject/Makefile-Debug.mk:78: recipe for target 'build/Debug/GNU-Linux/main.o' failed
make[2]: *** [build/Debug/GNU-Linux/main.o] Error 1
make[2]: Leaving directory '/home/manu/NetBeansProjects/GameMenu'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/manu/NetBeansProjects/GameMenu'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 953ms)

【问题讨论】:

  • 您告诉std::bind 在调用pressEnter 函数时将变量button 作为参数传递给它。然而你的函数不接受任何参数。
  • 我知道对吗?我尝试的第一件事是设置pressEnter 来接收参数,因为它看起来很奇怪,但它不起作用。另外,教程保持这种方式。
  • 如果你有错误,首先显示给出错误的代码。然后还包括你得到的实际错误,完整的,完整的可能的信息注释,并且未经编辑。请花一些时间到read about how to ask good questions
  • 非常感谢您的建议。前段时间我实际上红了指南,但看起来我忘记了最重要的部分。不过,感谢您抽出宝贵的时间,我已经编辑了我的帖子并添加了两行中的每一行的错误,以及控制台的完整输出。

标签: c++ sfml sfgui


【解决方案1】:

一种选择是将您的变量放在一个双方都可以访问它的范围内。

sfg::Button::Ptr button;

void pressEnter(){
    button->SetLabel("Tocado");
}

int main(){

    sfg::SFGUI sfgui;

    sf::VideoMode video_mode( 800, 600 );
    sf::RenderWindow rwindow( video_mode, "In-joked - Menu Principal");
    sf::Event event;

    button = sfg::Button::Create("Status");
    button->GetSignal(sfg::Button::OnLeftClick).Connect(&pressEnter);

    ...
}

这是一个全局变量,可能不是最好的方法,但它会起作用。对于此处的问答,如何构建您的程序以使其不包含全局变量有点过于宽泛。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-11
    • 2014-10-30
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多