2014-01-22 68 views
0

嗨我有一个问题编译下面的代码。我使用auto和std :: bind来绑定一个带参数的回调函数。但是,在将此回调函数作为参数传递之后,它会出现编译问题。你看到的问题与下面的函数声明:用C++ 11编译错误std :: bind和auto为回调函数参数

#include <iostream> 
#include <functional> 
using namespace std; 

class VmapPlayer 
{ 
    public: 
     void startPlayback(); 
     void playAdBreak(int adBreak, void (VmapPlayer::*callback)()); 
     void playSingleAd(int ad, void (VmapPlayer::*callback)(int adBreak, void (VmapPlayer::*cb)())); 
}; 

void VmapPlayer::playSingleAd(int ad, void (VmapPlayer::*callback)(int adBreak, void (VmapPlayer::*cb)())) 
{ 
    cout << "i am here" << endl; 

    // OPTION #1 I would like to call this function 
    //(this->*callback)(adBreak, cb); 

    // OPTION #2 I would like this call this function without the params: 
    //(this->*callback)(); 
} 

void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)()) 
{ 
    auto cb = std::bind(&VmapPlayer::playAdBreak, adBreak, callback); 
    playSingleAd(123, cb); 
} 

void VmapPlayer::startPlayback() 
{ 
    playAdBreak(456, &VmapPlayer::startPlayback); 
} 

int main() 
{ 
    VmapPlayer p; 
    p.startPlayback(); 

    return 0; 
} 

请参阅下面的编译错误日志:

main.cpp||In member function 'void VmapPlayer::playAdBreak(int, void (VmapPlayer::*)())':| 
main.cpp|28|error: no matching function for call to 'VmapPlayer::playSingleAd(int, std::_Bind<std::_Mem_fn<void (VmapPlayer::*)(int, void (VmapPlayer::*)())>(int, void (VmapPlayer::*)())>&)'| 
main.cpp|28|note: candidate is:| 
main.cpp|14|note: void VmapPlayer::playSingleAd(int, void (VmapPlayer::*)(int, void (VmapPlayer::*)()))| 
main.cpp|14|note: no known conversion for argument 2 from 'std::_Bind<std::_Mem_fn<void (VmapPlayer::*)(int, void (VmapPlayer::*)())>(int, void (VmapPlayer::*)())>' to 'void (VmapPlayer::*)(int, void (VmapPlayer::*)())'| 
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| 

我认为我的问题可以简化为:

playSingleAd()函数声明需要为了成功编译以下内容?:

void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)()) 
{ 
    auto cb = std::bind(&VmapPlayer::playAdBreak, adBreak, callback); 
    playSingleAd(123, cb); 
} 

回答

1

将方法绑定到完全提供的参数时,得到的仿函数不带任何参数。在你的代码中,playSingleAd接受一个函数指针作为参数,一个函数指针的参数在被调用时被提供。由于您已将参数绑定到该函数指针,因此不能在函数签名中指定其参数。

无论如何,您的代码可以使用std::function改进。此外,函数将不得不以实例为参数,如下面的playSingleAd的实现所示:

class VmapPlayer 
{ 
public: 
    void startPlayback(); 
    void playAdBreak(int adBreak, void (VmapPlayer::*callback)()); 
    void playSingleAd(int ad, std::function<void (VmapPlayer&)>); 
}; 

void VmapPlayer::playSingleAd(int, std::function<void (VmapPlayer&)> callback) 
{ 
    cout << "i am here" << endl; 
    callback(*this); 
} 

void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)()) 
{ 
    using namespace std::placeholders; 
    auto cb = std::bind(&VmapPlayer::playAdBreak, _1, adBreak, callback); 
    playSingleAd(123, cb); 
} 
+0

感谢您的回复。我不知道!啊,我更新了我的代码,但它似乎仍然无法编译。你看到我的函数声明有什么问题吗? – codeshark

+0

@ user1456962你可以发布错误消息吗? :) – 0x499602D2

+0

它指出问题在线“playSingleAd(123,cb);” - error:没有匹配函数调用'VmapPlayer :: playSingleAd()....“ – codeshark

相关问题