因此,我刚刚开始在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/