c++ - 使用 boost::bind 将成员函数绑定(bind)到 boost::bisect?

标签 c++ boost binding bisection

我遇到了 this 的问题以前但现在它以某种方式工作。

现在我遇到了以下问题。在使用相同的函数调用 boost::bisect 之前,我需要将值绑定(bind)到成员函数中。我发现还不错tutorial我已经关注了它,但似乎我仍然做错了什么。

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

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

在那之后我“在我认为它可以工作的时候即时”添加了绑定(bind)

 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,由于某些原因它不能作为具有绑定(bind)的成员函数。我以非成员(member)身份对其进行了测试,并且有效

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

boost::bind() 的参数是:

  1. 要绑定(bind)的函数(对象)的名称
  2. 要传递给它的参数,正如函数所期望的那样

对于你的情况,一个 CLASS::memberFunc(),它是一个 CLASS *(可能是 this 但任何 CLASS * 是可以的)作为第一个,您从字面上这样声明,然后是稍后传递给绑定(bind)对象的参数

这些“ future ”由 _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;
[ ... ]

关于c++ - 使用 boost::bind 将成员函数绑定(bind)到 boost::bisect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299107/

相关文章:

c++ - 二维数组操作和二进制搜索的实现

c++ - 不使用 * 和/运算符的两个 float 的乘法和除法

c++ - boost::atomic 与 boost::optional 不同的行为与 boost 1.55 和 1.58

c++ - 在 ‘)’ token C++ 之前预期为 ‘&’

WPF如何通过DynamicResource为FontSize属性设置pt(12pt等..)

sharepoint - 在复制器事件中绑定(bind) taskid 属性的正确方法是什么?

c++ - Qt - 应用程序代码的正确设计

c++ - 如何将数组保存到两个类方法中?

即使设置了 header 搜索路径并安装了 boost,Xcode pch 文件也无法找到 boost

c# - 绑定(bind)到不带参数的成员函数?