求助大神 !! 类函数模板 用函数做参数提示 student::bujige”: 非静态成员函数的非法调用
#include <iostream>#include <string>
class student;
template <typename T, class fun>
int my_count_if(T *a,int n,fun f) //f 是判断特征的函数
{
int count = 0;
for ( int i=0; i<n; ++i )
{
if (a[i].f)
{
count ++;
}
}
return count ;
}
class student
{
private:
int score;
std::string name;
public:
student(int a=0,char* s="")
:score(a),name(s){}
student (const student& s)
{
if (this!=&s)
{
this->name=s.name;
this->score=s.score;
}
}
student &operator=(const student&rhs)
{
if (this!=&rhs)
{
this->name=rhs.name;
this->score=rhs.score;
}
return *this;
}
bool bujige() //定义不及格的函数
{
return score<60;
}
};
int main()
{
student a[]={student(50,"tom"),student(49,"jerry"),student(91,"henry")};
int n=sizeof(a)/sizeof(*a);
int num=my_count_if(a,n,student::bujige);
}
//其中bujige是作为一个判定性质的函数