1

我试图弄清楚活页夹是如何工作的。我正在研究HERE的示例。所以我决定扩展一点,但我不知道出了什么问题

namespace mine {
   using std::bind1st;
   using std::bind2nd;
   using std::function;
};

struct Foo {
    Foo(int num) : num_(num) {}
    void print_add(int i) const { std::cout << num_+i << '\n'; }
    int num_;
};


int main()
{

using namespace mine;

    // store a call to a member function
    function<void(const Foo&, int)> f_add_display = &Foo::print_add;
    Foo foo(314159);
    f_add_display(foo, 1);

    function<void(int)> foo_f_add_display = bind1st(f_add_display, foo);
    foo_f_add_display(1);

    // The problem is here  <------------
    function<void(const Foo&)> f_1_add_display = bind2nd(f_add_display, 1);
    // f_1_add_display(foo);
}

我收到的错误消息(来自 Intel CC,gdb 无法理解)

Compilation finished with errors:
c++/4.7/backward/binders.h(160): error: invalid redeclaration of member function "std::binder2nd<_Operation>::operator()(const _Operation::first_argument_type &) const [with _Operation=std::function<void (const Foo &, int)>]" (declared at line 154)
operator()(typename _Operation::first_argument_type& __x) const
^
detected during instantiation of class "std::binder2nd<_Operation> [with _Operation=std::function<void (const Foo &, int)>]" at line 41 of "source.cpp"

compilation aborted for source.cpp (code 2)

这里到底有什么问题。为什么不能绑定第二个参数?还是只是一些语法错误或什么?

如果有人需要,代码就在这里。

4

0 回答 0