gpt4 book ai didi

c++ - 回调函数参数的 C++11 std::bind 和 auto 编译错误

转载 作者:行者123 更新时间:2023-11-28 07:09:09 24 4
gpt4 key购买 nike

您好,我在编译以下代码时遇到问题。我正在使用 auto 和 std::bind 将回调函数与参数绑定(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);
}

最佳答案

当您将方法绑定(bind)到完全提供的参数时,生成的仿函数不接受任何参数。在您的代码中,playSingleAd 采用一个函数指针作为参数,该函数指针的参数将在调用时提供。因为您已经将参数绑定(bind)到该函数指针,所以不能在函数签名中指定其参数。

无论如何,您的代码可以通过使用 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);
}

关于c++ - 回调函数参数的 C++11 std::bind 和 auto 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21293787/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com