使用boost ::绑定绑定成员函数,以提高::平分? [英] Using boost::bind to bind member-function to boost::bisect?

查看:180
本文介绍了使用boost ::绑定绑定成员函数,以提高::平分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经与在此之前但现在它莫名其妙地工作。

现在,我下面的问题。我需要在我打电话的boost ::具有相同功能的平分到值绑定到成员函数。我发现pretty好教程,然后我跟着,但它似乎我还是做错了。

起初,我创建测试类在那里我得到了以下工作:

 的std ::对<双层,双>结果=平分(安培;函数功能,0.0,1.0,TerminationCondition());
            双根=(result.first + result.second)/ 2;

在我添加绑定对飞因为我认为它可以工作

 的std ::对<双层,双>结果=平分(提高::绑定(安培; CLASS ::功能,为此,_1),0.0,1.000000,TerminationCondition());

那结果是一个错误。错误:终止叫做抛出的一个实例后'的boost :: exception_detail :: clone_impl>是什么():错误函数boost ::数学::工具::平分:在boost ::数学::工具符号没有变化: :平分,要么没有根找到,或有重根在区间(F(分钟)= -0.0032916729090909091)

反正这里是类::功能不作为成员函数出于某种原因,结合工作。我测试了它作为非成​​员和它的作品

  Double类::功能(双c)
{
/ *值:M_A,M_B,M_C,M_D和m_minus位于.h文件中* /正常规范;
双TEMP = * M_D的sqrt(C);双总=((1-的boost ::数学:: PDF(常态,(M_A * C + M_B)/温度)) - (1 - M_C)+提振::数学:: PDF(常态,(M_A * C + M_B)/ TEMP));收益率(总 - m_minus);
}


解决方案

如果我正确读取教程,它应该是:

 的std ::对<双层,双>结果=
    对分(提高::绑定(安培; CLASS ::功能,为此,_1,_2,_3)
        0.0,1.000000,TerminationCondition());

即。参数以的boost :: bind()的是:


  1. 函数(对象)的名称绑定到

  2. 的参数传递给作为的功能的希望他们

有关你的情况,一个类:: memberFunc(),那将会是一种类* (可能这个但是任何类* 是确定)作为第一个,你从字面上国家本身,其次是参数之后传递到绑定的对象

这些期货是由 _1 _2 等,这取决于它们的位置在调用时指定

例如:

 类addthree {
私人的:
    诠释秒;
上市:
    addthree(中间体term2nd = 0):秒(term2nd){}
    无效AddTo就(INT和放大器; term1st,const int的constval){
        term1st + =(term2nd + constval);
    }
}int类型的;
addthree AA;
提振::函数<无效(INT)> add_to_a =的boost ::绑定(安培; addthree :: AddTo就,&安培; AA,A,_1);
提振::函数<无效(无效)> inc_a =的boost ::绑定(安培; addthree :: AddTo就,&安培; AA,A,1);一个= 0; add_to_a(2);性病::法院LT&;< A<<的std :: ENDL;
一个= 10; add_to_a(一);性病::法院LT&;< A<<的std :: ENDL;
一个= 0; inc_a();性病::法院LT&;< A<<的std :: ENDL;
[...]

I've had problems with this before but now it's somehow working.

Now I've following problem. I need to bind values into member-function before I call boost::bisect with the same function. I found pretty good tutorial and I've followed it but it seems that I'm still doing something wrong.

At first I created test class where I got following working:

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

After that I added binding "on the fly as I thought it could work"

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

Result of that was an error. error: terminate called after throwing an instance of 'boost::exception_detail::clone_impl >' what(): Error in function boost::math::tools::bisect: No change of sign in boost::math::tools::bisect, either there is no root to find, or there are multiple roots in the interval (f(min) = -0.0032916729090909091).

Anyway here is class::function which doesn't work as member function with binding for some reason. I tested it as non-member and it works

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); 
}

解决方案

If I read the tutorial correctly, it should be:

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

I.e. the parameters to boost::bind() are:

  1. The name of the function (object) to bind to
  2. the arguments to pass to that, as the function expects them

For your case, a CLASS::memberFunc(), that'd be a CLASS * (possibly this but any CLASS * is ok) as the first, which you literally state as such, followed by the parameters later passed to the bound object.

These "futures" are designated by _1, _2 and so on, depending on their position at invocation time.

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

这篇关于使用boost ::绑定绑定成员函数,以提高::平分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆