[英]No suitable conversion user-defined conversion with std::bind()
我正在尝试实现一个 Button 类,该类在单击按钮时调用回调。
class Button
{
public:
void SetOnMouseClickCallback(std::function<void()> f);
// Some other stuff
private:
std::function<void()> callback;
};
using ButtonPtr = std::unique_ptr< Button >;
void Button::SetOnMouseClickCallback(std::function<void()> f)
{
callback = f;
}
在我的 App 类中,我想初始化所有按钮并最终为它们分配一个回调:
void App::Initialize()
{
ButtonPtr b = std::make_unique<Button>(100.f, 100.f, 200.f, 50.f, "texture.jpg");
b->SetOnMouseClickCallback(std::bind(&Foo)); // Error here under std::bind
}
void App::Foo()
{
cout<<"bar";
}
我已经被困在这里一整天了,我已经不知道了。 希望您能够帮助我。 提前致谢。
Foo 不是void()
。 您忘记将其绑定到当前实例:
std::bind(&App::Foo, this)
非静态成员函数具有隐式函数参数,即要调用的对象。 因此,您需要传递bind
要调用的对象。 这意味着你应该使用
b->SetOnMouseClickCallback(std::bind(&Foo, this));
如果要使用当前对象或
App call_on;
b->SetOnMouseClickCallback(std::bind(&Foo, &call_on));
如果您希望它拥有自己的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.