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

前端 未结 2 1251
情深已故
情深已故 2021-01-25 06:12

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 wi

相关标签:
2条回答
  • 2021-01-25 06:17

    This code:

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

    is correct. The error you get means that what CLASS::Function returns is invalid. bisect is complaining about multiple roots (or possibly no roots) in the given interval [0; 1]. How does CLASS::Function look like?

    0 讨论(0)
  • 2021-01-25 06:31

    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;
    [ ... ]
    
    0 讨论(0)
提交回复
热议问题