读书人

bind3仿函数出了有关问题

发布时间: 2012-03-20 14:01:10 作者: rapoo

bind3仿函数出了问题?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template <class _T,class _Res=bool >
struct mybeetwen
{ typedef _Res ResType; typedef _T first_Arg1;
_Res operator ()(const _T &x ,const _T&a,const _T&b) { return (x> =a) && (x <=b) ;} };


template <class Operation,class T>
class mybind3
{
typedef typename Operation::ResType Res;
typedef typename Operation::first_Arg1 Arg1;
explicit mybind3(const Operation & Opx,const T& x,const T&y):Op(Opx),A(x),B(y) {}
Res operator ()(const Arg1& data) {return Op(data,A,B); }
private:
T A,B; Operation Op;
};


int main()
{
vector <int> v;
v.push_back(10);v.push_back(15);v.push_back(19);v.push_back(20);v.push_back(14);
int count=count_if(v.begin(),v.end(), mybind3(mybeetwen(),10,20));
cout < <count;
}
error C2955: “mybeetwen”: 使用类 模板 需要 模板 参数列表
e:\c++分支\aaaa\aaaa\ffffff.cpp(9) : 参见“mybeetwen”的声明
e:\c++分支\aaaa\aaaa\ffffff.cpp(30) : error C2955: “mybind3”: 使用类 模板 需要 模板 参数列表
e:\c++分支\aaaa\aaaa\ffffff.cpp(21) : 参见“mybind3”的声明
生成日志保存在“file://e:\C++分支\aaaa\aaaa\Debug\BuildLog.htm”

[解决办法]
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template <class _T,class _Res=bool >
struct mybeetwen
{
typedef _Res ResType;
typedef _T first_Arg1;
_Res operator ()(const _T &x ,const _T&a,const _T&b)
{
return (x> =a) && (x <=b) ;
}
};


template <class Operation,class T>
class mybinder3
{
public:
typedef typename Operation::ResType Res;
typedef typename Operation::first_Arg1 Arg1;
explicit mybinder3(Operation Opx,T x,T y):Op(Opx),A(x),B(y) {}
Res operator ()(const Arg1& data) {return Op(data,A,B); }
private:
T A,B; Operation Op;
};

template <class Operator,class T>
inline mybinder3 <Operator,T>
mybind3 (Operator x,T a,T b)
{
return mybinder3 <Operator,T> (x,a,b );
}


int main()
{
vector <int> v;
v.push_back(10);v.push_back(15);v.push_back(19);v.push_back(20);v.push_back(14);


int count=count_if( v.begin(),v.end(), mybind3( mybeetwen <int,bool> (),10,20) );
cout < <count;
}
[解决办法]
你的换行都到哪里去了 - -

改了下


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template <class _T, class _Res = bool>
struct mybeetwen
{
typedef _Res ResType;
typedef _T first_Arg1;

_Res operator () (const _T &x ,const _T&a,const _T&b)
{
return (x> =a) && (x <=b);
}
};


template <class Operation>
class mybinder3
{
public :
typedef typename Operation::ResType Res;
typedef typename Operation::first_Arg1 Arg;
explicit mybinder3(const Operation & Opx,const Arg& x,const Arg&y):Op(Opx),A(x),B(y) {}
Res operator ()(Arg data)
{
return Op(data,A,B);
}
private:
Arg A,B;
Operation Op;
};

template <class Operator,class T1,class T2>
inline mybinder3 <Operator> mybind3 (const Operator & x,const T1& a,const T2& b)
{
typedef typename Operator::first_Arg1 Arg;
return mybinder3 <Operator> (x,(Arg)a,(Arg)b );
}


int main()
{
vector <int> v;
v.push_back(10);
v.push_back(15);
v.push_back(19);
v.push_back(20);
v.push_back(14);
size_t count = count_if(v.begin(), v.end(), mybind3(mybeetwen <int> (), 10, 20));
cout < <count;

return 0;
}



[解决办法]
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

template <class _T,class _Res=bool >
struct mybeetwen
{ typedef _Res ResType; typedef _T first_Arg1;
_Res operator ()(const _T &x ,const _T&a,const _T&b) { return (x> =a) && (x <=b) ;} };


template <class Operation,class T>
class mybind3
{
public:
typedef typename Operation::ResType Res;
typedef typename Operation::first_Arg1 Arg1;
explicit mybind3(const Operation & Opx,const T& x,const T&y):Op(Opx),A(x),B(y) {}
Res operator ()(const Arg1& data) {return Op(data,A,B); }
private:
T A,B; Operation Op;
};


void main()
{
vector <int> v;
v.push_back(10);v.push_back(15);v.push_back(19);v.push_back(20);v.push_back(14);
int count=count_if(v.begin(),v.end(), mybind3 <mybeetwen <int> ,int> (mybeetwen <int> (),10,20));
cout < <count;
}

读书人网 >C++

热点推荐