c++ - 试图将一个简单的按钮绑定(bind)到 SFGUI 中的 OnClick 函数

标签 c++ sfml sfgui

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

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

在库的大多数示例中,这个问题是通过使用 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)

最佳答案

一种选择是将您的变量置于两者都可以访问的范围内。

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);

    ...
}

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

关于c++ - 试图将一个简单的按钮绑定(bind)到 SFGUI 中的 OnClick 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42812997/

相关文章:

c++ - SFML 平台游戏与 map 图 block 发生碰撞

c++ - 为什么编译器不能从返回类型推导出模板参数?

c++ - 图实现 : variable has initializer but incomplete type

c++ - Qt如何隐藏横条

c++ - 如何在 C++ 中将函数中的 STL 列表作为参数传递

python - 使 pySFML 在 Linux 上工作

c++ - 在 SWIG 中获取 SFML 跨平台类型定义