gpt4 book ai didi

c++ - boost::bind 不适用于指针参数

转载 作者:太空狗 更新时间:2023-10-29 21:29:48 25 4
gpt4 key购买 nike

我有这个简单的程序。在这里,我尝试将成员函数与对象绑定(bind),稍后使用成员函数调用所需的参数进行调用。当成员函数采用指向整数的指针时,gcc 无法编译。使用整数参数,程序将被编译。这是 boost::bind 的错误还是我遗漏了什么?

// Doesn't work with pointer
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
float f_;
float& f(int *aa) {
cout << *aa << endl;
return f_;
}
};

int main() {
X x;

x.f_=200.1;
int j = 10;

cout << bind(&X::f, ref(x), _1)(&j) << endl;
}

ERROR:
member.cpp: In function ‘int main()’:
member.cpp:22: error: no match for call to ‘(boost::_bi::bind_t<float&, boost::_mfi::mf1<float&, X, int*>, boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> > >) (int*)’
/usr/include/boost/bind/bind_template.hpp:17: note: candidates are: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() [with R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:23: note: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()() const [with R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:29: note: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = int*, R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]
/usr/include/boost/bind/bind_template.hpp:35: note: typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) const [with A1 = int*, R = float&, F = boost::_mfi::mf1<float&, X, int*>, L = boost::_bi::list2<boost::reference_wrapper<X>, boost::arg<1> >]

// Works fine
#include <iostream>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;

struct X
{
float f_;
float& f(int aa) {
cout << aa << endl;
return f_;
}
};

int main() {
X x;

x.f_=200.1;
int j = 10;

cout << bind(&X::f, ref(x), _1)(j) << endl;
}

最佳答案

bind 的第一个参数应该是指向应用非静态方法的对象的指针:

cout << bind(&X::f, &x, _1)(&j) << endl;  

关于c++ - boost::bind 不适用于指针参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4076405/

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