2016-05-30 11 views
1

OIS :: Keys(int)とstd :: functionの配列をしたいです。メンバ関数をstd ::関数にバインドする方法はありますか?

私はこれがあります。

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

何をしましたか?

+3

多分 'のstd ::バインド(&変数UserInput :: selectBazooka(代わりにstd::bind()の)このようなものです、 &input、std :: placeholders :: _ 1、std :: placeholders :: _ 2) ' –

+3

ラムダを使用していない理由はありますか? (それはバインドよりもコードを明確にします) – Borgleader

+1

PiotrSkotnicki thanskはうまくいきます! @Borgleaderラムダはここでどのように役立つのでしょうか? –

答えて

5

std::bindの最初の引数は、呼び出し可能なオブジェクトです。あなたの場合、それは&UserInput::selectBazookaでなければなりません。そのメンバー関数(&input)への呼び出しに関連付けられるオブジェクトは、後で(この順序を逆にして)進みます。それでも、あなたが不足しているパラメータのプレースホルダを使用する必要があります。

std::bind(&UserInput::selectBazooka, &input, std::placeholders::_1, std::placeholders::_2) 
6

ラムダを使用して、

[&](Worms*x, const Ogre::FrameEvent&y) { return input.selectBazooka(x,y); } 
関連する問題