使用binder1st和自定义函子

cpp*_*cpp 1 c++ functor

我想将我的print functor的第一个参数绑定到0:

#include<iostream>
#include<functional>
using namespace std;

class Print : public std::binary_function<int,int,void>{
public:
    void operator()(int val1, int val2)
    {   
        cout << val1 + val2 << endl;
    }   
};

int main()
{
    Print print;
    binder1st(print,0) f; //this is line 16
    f(3);  //should print 3
}
Run Code Online (Sandbox Code Playgroud)

上面的程序(基于C++ Primer Plus的示例)无法编译:

line16 : error : missing template arguments before ‘(’ token
Run Code Online (Sandbox Code Playgroud)

怎么了?

我不想使用C++ 11也不想提升功能.

编辑:为简单起见,operator()返回类型已从bool更改为void

doc*_*ove 5

正如错误消息所示,在( 您想要的之前,您缺少模板参数

std::binder1st<Print> f(print, 0);
Run Code Online (Sandbox Code Playgroud)

但是,您还需要使operator()const如下所示

bool operator()(int val1, int val2) const
Run Code Online (Sandbox Code Playgroud)

最后,这个函数需要返回一些东西.