class Student {
// ...
bool Graduate() { return m_bGraduate; }
// ...
};
class School {
vector<Student*> m_vecStudents;
void DelAndNullify(Student* &pStd);
void Fun1();
};
void School::DelAndNullify(Student* &pStd)
{
if ( (pStd != NULL) && (pStd->Graduate()) )
{
delete pStd;
pStd = NULL;
}
}
void School::Fun1()
{
for_each(m_vecStudents.begin(), m_vecStudents.end(), mem_fun(&School::DelAndNullify));
}
错误1错误C2064:term不评估为带有1个参数的函数C:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ include \ algorithm 22 1模拟
为什么我会收到此错误?
将Student
更改为pStd
template<class _InIt, class _Fn1> inline
_Fn1 _For_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{
// perform function for each element
for (; _First != _Last; ++_First)
_Func(*_First); // <<<<<<<< this line!
return (_Func);
}
BTW,如果我将DelAndNullify
定义为static
,则以下行传递编译器
for_each(m_vecStudents.begin(), m_vecStudents.end(), ptr_fun(&School::DelAndNullify));
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <iostream>
#include <iomanip>
#include <functional>
#include <boost/bind.hpp>
class Student {
public:
Student(int id, bool bGraduate) : m_iID(id), m_bGraduate(bGraduate) {}
bool Graduate() const { return m_bGraduate; }
private:
int m_iID;
bool m_bGraduate;
};
class School {
public:
School(int numStudent)
{
for (int i=0; i<numStudent; ++i)
{
m_vecStudents.push_back(new Student(i+1, false));
}
}
~School()
{
// deallocate the allocated student resource to prevent memory leak!
}
void DelAndNullify(Student* &pStd);
void Fun1();
private:
std::vector<Student*> m_vecStudents;
};
void School::DelAndNullify(Student* &pStd)
{
if ( (pStd != NULL) && (!pStd->Graduate()) )
{
delete pStd;
pStd = NULL;
}
}
void School::Fun1()
{ // http://stackoverflow.com/questions/6065041/error-c2064-term-does-not-evaluate-to-a-function-taking-1-arguments
std::for_each(m_vecStudents.begin(), m_vecStudents.end(), std::bind1st(std::mem_fun(&School::DelAndNullify), this));
//boost::bind(&School::DelAndNullify, this, _1);
}
int main(int /*argc*/, char* /*argv*/[])
{
School school(10);
school.Fun1();
return 0;
}
错误1错误C2535:'void std :: binder1st&lt; _Fn2&gt; :: operator()(学生 *&amp;)const':已定义或声明的成员函数c:\ Program Files \ Microsoft Visual Studio 10.0 \ VC \ include \ xfunctional 299
答案 0 :(得分:6)
std::mem_fun(&School::DelAndNullify)
会返回一个带有School*
和Student*
的二元仿函数,但std::for_each
期望一元仿函数仅使用Student*
。请改用Boost。Bind:
std::for_each(
m_vecStudents.begin(),
m_vecStudents.end(),
boost::bind(&School::DelAndNullify, this, _1)
);
如果你有一个足够新的编译器,那么你可以使用std::bind
或std::tr1::bind
而不是Boost库;或者,如果您使用的是具有C ++ 11 lambda支持的编译器,那么您可以执行以下操作,而不是使用任何bind
:
std::for_each(
m_vecStudents.begin(),
m_vecStudents.end(),
[this](Student*& s){ DelAndNullify(s); }
);
答案 1 :(得分:1)
看起来mem_fun
将您的成员函数转换为“静态”函数,该函数将对象作为其第一个参数,如:
static void DelAndNullfify(Student *pStudent);
但是你已经在pre-mem_fun'd函数中有了一个参数,所以你最终会得到:
static void DelAndNullfify(School *pSchool, Student* &prStudent);
这个参数太多了。
答案 2 :(得分:1)
此
mem_fun(&School::DelAndNullify)
返回二进制函数,期待School*
和Student*
。
使用
bind1st(mem_fun(&School::DelAndNullify), this)
代替。