您好我在编译以下代码时遇到问题。我正在使用 auto 和 std::bind 来绑定带有参数的回调函数。但是,在将此回调函数作为参数传递后,它会出现编译问题。您是否看到以下函数声明存在问题:
#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);
}