C++ 使用mem_fun_ref()传递用户定义的成员函数
我完成了将普通方法和函子作为参数的传递,不管我是如何被成员变量的传递所困扰C++ 使用mem_fun_ref()传递用户定义的成员函数,c++,function,class-members,C++,Function,Class Members,我完成了将普通方法和函子作为参数的传递,不管我是如何被成员变量的传递所困扰 #include <iostream> #include <string> #include <vector> #include <functional> #include <algorithm> using namespace std; class stamp //:virtual public unary_function<const int,
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
class stamp //:virtual public unary_function<const int,bool>
{
public:
bool stmp(const int vl) const
{
return false;
}
};
template<class T>
int fncatcher(T fun)
{
fun(1);
return 0;
}
int main()
{
vector<string> names;
fncatcher(mem_fun_ref(&stamp::stmp));//THIS DOES NOT WORK!
names.push_back("GUf");
names.push_back("");
names.push_back("Dawg");
names.push_back("");
cout<<count_if(names.begin(),names.end(),mem_fun_ref(&string::empty))<<endl; //THIS WORKS
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类戳//:虚拟公共一元函数
{
公众:
bool stmp(const int vl)const
{
返回false;
}
};
模板
国际捕手(T乐趣)
{
乐趣(1);
返回0;
}
int main()
{
载体名称;
fncatcher(mem_-fun_-ref(&stamp::stmp));//这不起作用!
姓名。推回(“GUf”);
名称。推回(“”);
姓名。推回(“Dawg”);
名称。推回(“”);
您是否需要在实例上调用成员函数
template<class T>
int fncatcher(T fun)
{
stamp s;
fun(s, 1);
return 0;
}
模板
国际捕手(T乐趣)
{
邮票;;
乐趣(s,1);;
返回0;
}
或者将其设置为静态:
class stamp //:virtual public unary_function<const int,bool>
{
public:
static bool stmp(const int vl) const
{
return false;
}
};
class stamp//:虚拟公共一元函数
{
公众:
静态布尔stmp(常量int vl)常量
{
返回false;
}
};
@MauriceRodriguez:“似乎”是什么意思?它修复了编译错误吗?请参阅我的更新-第一个示例是错误的。
class stamp //:virtual public unary_function<const int,bool>
{
public:
static bool stmp(const int vl) const
{
return false;
}
};