gpt4 book ai didi

bind - std::bind 具有多个参数的成员函数

转载 作者:行者123 更新时间:2023-12-02 04:28:54 27 4
gpt4 key购买 nike

我有这个代码

struct A {
void f(int) {}
void g(int, double) {}
};

int main() {
using std::placeholders;
A a;

auto f1 = std::bind(&A::f, &a, _1);
f1(5); // <--- works fine

auto f2 = std::bind(&A::g, &a, _1);
f2(5, 7.1); // <--- error!
}

我从编译器(gcc 4.8.1)收到此错误:

error: no match for call to '(std::_Bind<std::_Mem_fn<void (A::*)(int, double)>(A*, std::_Placeholder<1>)>) (int, double)'
f2(1, 1.1);
^

你能告诉我错误在哪里吗?

谢谢

马西莫

最佳答案

调用bind需要指定两个参数,如下所示:

auto f2 = std::bind(&A::g, &a, _1, _2);

关于bind - std::bind 具有多个参数的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775289/

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