首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

vc编译失误:error c2664(mem_fun_ref函数) 求解

2013-07-16 
vc编译出错:error c2664(mem_fun_ref函数)求解本帖最后由 u011386525 于 2013-07-12 17:13:34 编辑在看c++

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;
}

编译出错:error C2664: 'mem_fun_ref' : cannot convert parameter 1 from 'void (__thiscall Person::*)(void) const' to 'void (__thiscall Person::*)(void)'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast VC 类
[解决办法]
没有问题啊!是不是你没有配置为C++而是C呢。

热点排行