所以,我刚开始在我的SFML项目中使用SFGUI,而且我很难理解一切是如何工作的。我一直在阅读hello world教程以及图书馆本身在其文件夹中提供的一些示例。
我现在正在尝试创建一个简单的按钮,当它检测到左键单击时会执行某些操作,但我甚至无法正常工作。错误窗口显示我的button
方法中未定义OnClick
变量,如果我尝试使用button
的参数发送std::bind
,我会得到典型的&#34 ;没有匹配功能..."。
在大多数库的示例中,通过在函数名称旁边使用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)
答案 0 :(得分: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);
...
}
这是一个全局变量,可能不是最好的方法,但它会起作用。如何将程序结构化为没有全局变量对于Q&amp; A来说有点过于宽泛。