我想做一個OIS :: Keys(int)和std :: function的數組。如何將成員函數綁定到std :: function?
我有這樣的:
struct UserCommands
{
OIS::KeyCode key;
std::function<bool(Worms *, const Ogre::FrameEvent& evt)> func;
};
UserInput input;
UserCommands usrCommands[] =
{
{
OIS::KC_A, std::bind(&input, &UserInput::selectBazooka)
},
};
但是當我嘗試編譯此我有這樣的編譯錯誤:
In file included from includes/WormsApp.hh:5:0,
/src/main.cpp:2:
/includes/InputListener.hh:26:25: error: could not convert ‘std::bind(_Func&&, _BoundArgs&& ...) [with _Func = UserInput*; _BoundArgs = {bool (UserInput::*)(Worms*, const Ogre::FrameEvent&)}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<UserInput*(bool (UserInput::*)(Worms*, const Ogre::FrameEvent&))>](&UserInput::selectBazooka)’ from ‘std::_Bind_helper<false, UserInput*, bool (UserInput::*)(Worms*, const Ogre::FrameEvent&)>::type {aka std::_Bind<UserInput*(bool (UserInput::*)(Worms*, const Ogre::FrameEvent&))>}’ to ‘std::function<bool(Worms*, const Ogre::FrameEvent&)>’
OIS::KC_A, std::bind(&input, &UserInput::selectBazooka)
^
我做了什麼錯?
也許'的std ::綁定(UserInput :: selectBazooka, &input,std :: placeholders :: _ 1,std :: placeholders :: _ 2)' –
有什麼理由不使用lambda? (imo它使代碼更清晰而不是綁定) – Borgleader
PiotrSkotnicki thansk,工作正常! @Borgleader lambda在這裏可能有用嗎? –