使用 boost::bind 将成员函数绑定到 boost::bisect?

2024-03-22

我遇到了问题this https://stackoverflow.com/questions/8245909/how-to-use-boost-bisection以前但现在它在某种程度上起作用了。

现在我有以下问题。在使用相同的函数调用 boost::bisect 之前,我需要将值绑定到成员函数中。我发现还不错tutorial http://www.radmangames.com/programming/how-to-use-boost-bind我已经遵循了,但似乎我仍然做错了什么。

首先,我创建了测试类,我在其中进行了以下工作:

std::pair<double, double> result = bisect(&Func, 0.0, 1.0, TerminationCondition());
            double root = (result.first + result.second) / 2;

之后,我“即时添加了绑定,因为我认为它可以工作”

 std::pair<double, double> result = bisect(boost::bind(&CLASS::Function,this, _1), 0.0, 1.000000, TerminationCondition());

结果是一个错误。错误:抛出 'boost::exception_detail::clone_impl >' What() 实例后终止调用:函数 boost::math::tools::bisect 中出现错误:boost::math::tools 中的符号没有变化: :bisect,要么没有根可求,要么区间内有多个根(f(min) = -0.0032916729090909091)。

无论如何,这里的 class::function 由于某种原因不能作为具有绑定的成员函数。我以非会员身份测试过,可以用

double CLASS::Function(double c)
{
/* values: m_a, m_b, m_c, m_d, and m_minus are located in .h file */

normal norm;
double temp = m_d*sqrt(c);

double total = ((1-boost::math::pdf(norm,(m_a*c+m_b)/temp))-(1 - m_c)+boost::math::pdf(norm,(m_a*c+m_b)/temp));

return (total - m_minus); 
}

如果我正确阅读教程的话,应该是:

std::pair<double, double> result =
    bisect(boost::bind(&CLASS::Function, this, _1, _2, _3),
        0.0, 1.000000, TerminationCondition());

IE。参数为boost::bind() are:

  1. 要绑定的函数(对象)的名称
  2. 传递给它的参数,如功能期待他们

对于您的情况,CLASS::memberFunc(),那将是一个CLASS *(可能this但任何CLASS *是好的)作为第一个,你字面意思是这样的,然后是参数稍后传递给绑定对象.

这些“期货”由_1, _2依此类推,具体取决于它们在调用时的位置。

Example:

class addthree {
private:
    int second;
public:
    addthree(int term2nd = 0) : second(term2nd) {}
    void addto(int &term1st, const int constval) {
        term1st += (term2nd + constval);
    }
}

int a;
addthree aa;
boost::function<void(int)> add_to_a = boost::bind(&addthree::addto, &aa, a, _1);
boost::function<void(void)> inc_a = boost::bind(&addthree::addto, &aa, a, 1);

a = 0 ; add_to_a(2); std::cout << a << std::endl;
a = 10; add_to_a(a); std::cout << a << std::endl;
a = 0 ; inc_a(); std::cout << a << std::endl;
[ ... ]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 boost::bind 将成员函数绑定到 boost::bisect? 的相关文章

随机推荐