Using boost :: bind to bind a member function to boost :: bisect?

I had problems with this , but now it somehow works.

Now I have a problem. I need to bind values ​​to a member function before I call boost :: bisect with the same function. I found a pretty good tutorial and I followed it, but it seems like I'm still doing something wrong.

First I created a test class, where I got the following work:

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

After that, I added the snap "on the fly, as I thought it might work"

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

The result of this was a mistake. Error: ending the call after calling the instance "boost :: exception_detail :: clone_impl>" what (): Error in the function boost :: math :: tools :: bisect: Without changing the sign in boost :: math :: tools :: bisect, either there is no root to search, or in the interval there are several roots (f (min) = -0.003291672909090909091).

In any case, there is a :: function class, which for some reason does not work as a member function with a binding. I tested it as a 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); 
}
+1
source share
2 answers

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

those. parameters boost::bind():

  • The name of the function (object) to bind to
  • , ,

a CLASS::memberFunc(), CLASS * (, this, CLASS * ) , , .

"" _1, _2 .., .

:

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

:

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

. , CLASS:: Function . bisect (, , ) [0; 1]. CLASS::Function?

+1

All Articles