1

print ファンクターの最初の引数を 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
}

上記のプログラム ( C++ Primer Plusの例に基づく) はコンパイルされません。

line16 : error : missing template arguments before ‘(’ token

なにが問題ですか?

C++11 もブースト機能も使いたくありません。

編集: 簡単にするために、operator() の戻り値の型が bool から void に変更されました

4

3 に答える 3

2

binder1stテンプレート引数が必要です。試してみてください

 binder1st<Print> f(print, 0);

こちらのリファレンスを参照してください。

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

int main () {
  binder1st < equal_to<int> > equal_to_10 (equal_to<int>(),10);
  int numbers[] = {10,20,30,40,50,10};
  int cx;
  cx = count_if (numbers,numbers+6,equal_to_10);
  cout << "There are " << cx << " elements equal to 10.\n";
  return 0;
}
于 2013-08-16T08:33:44.600 に答える
2

std::binder1stはクラス テンプレートであるため、テンプレート パラメーターが必要です。

binder1st<Print> f(print,0);
//       ^^^^^^^

しかし、本当に 2 番目の引数をバインドしたい場合は、適切な名前の を使用する必要がありますstd::binder2nd

于 2013-08-16T08:33:59.853 に答える