类成员函数作为函数参数出现error C3867:非标准语法;请使用“&”来创建指向成员的指针
有的时候一个泛函可能会采用函数或函数对象(functor)做参数,这时候我们可能想要使用类的成员函数来做参数,但是这时候会出现error C3867: 非标准语法;请使用 "&" 来创建指向成员的指针,比如下面例子:
using namespace std;
class TestC
{
private:
double a, b;
public:
TestC(double a_,double b_):a(a_),b(b_){}
double testFun(double x);
};
template<class Func>
double testTemplateFun(double input, Func proccess)
{
return proccess(input);
}
int main()
{
double a = 10;
TestC t(2, 3);
cout << "输出结果为:" << testTemplateFun(a, t.testFun);
system("pause");
return 0;
}
double TestC::testFun(double x)
{
return a*x + b;
}