[英]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.