vc编译出错:error c2664(mem_fun_ref函数) 求解
本帖最后由 u011386525 于 2013-07-12 17:13:34 编辑 在看c++ 标准程序,第8章的仿函数
内容是:定义类Person,private只有string name,public有构造函数,void print()const
想用for_each()以及mem_fun_ref()输出vector<Person>每一个Person对象的name
代码如下:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Person
{
private:
string name;
public:
Person(const string& m_name) :name(m_name) {
}
void print() const {
cout << name << endl;
}
};
int main()
{
vector<Person> v;
v.push_back(Person("aaaaa") );
v.push_back(Person("bbbbb") );
for_each(v.begin(), v.end(),
mem_fun_ref(&Person::print) //这里出错了
);
system("pause");
return 0;
}