错误C2064:term不评估为带有1个参数的函数 [英] error C2064: term does not evaluate to a function taking 1 arguments
问题描述
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 does not evaluate到一个函数取1个参数C:\Program Files \Microsoft Visual Studio 10.0 \VC\include\algorithm 22 1模拟
为什么会收到此错误?
p>变更学生
至 pStd
更新//算法文件
template< class _InIt,class _Fn1& inline
_Fn1 _For_each(_InIt _First,_InIt _Last,_Fn1 _Func)
{
//为每个元素执行函数
for(; _First!= _Last; ++ _ First)
_Func(* _ First); //<<<<<<<这行!
return(_Func);
}
template< class _InIt,class _Fn1& inline
_Fn1 _For_each(_InIt _First,_InIt _Last,_Fn1 _Func)
{
//为每个元素执行函数
for(; _First!= _Last; ++ _ First)
_Func(* _ First); //<<<<<<<这行!
return(_Func);
}
如果我定义 DelAndNullify
as static
然后下面的行通过编译器
for_each(m_vecStudents.begin(),m_vecStudents.end(),ptr_fun(& School :: DelAndNullify));
已更新05/09/2012
#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 {return m_bGraduate; }
private:
int m_iID;
bool m_bGraduate;
};
class School {
public:
School(int numStudent)
{
for(int i = 0; i {
m_vecStudents.push_back(new Student(i + 1,false));
}
}
〜School()
{
//解除分配的学生资源以防止内存泄漏!
}
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- do-not-evaluate-to-a-function-taking-1-arguments
std :: for_each(m_vecStudents.begin(),m_vecStudents.end(),std :: bind1st(std :: mem_fun(&学校:DelAndNullify),this));
// boost :: bind(& School :: DelAndNullify,this,_1);
}
int main(int / * argc * /,char * / * argv * / [])
{
school.Fun1();
return 0;
}
错误1错误C2535:'void std: :binder1st< _Fn2> :: operator()(Student
*&)const':已定义或声明的成员函数c:\Program Files \Microsoft Visual Studio 10.0 \VC\include\xfunctional 299
std :: mem_fun(& School :: DelAndNullify)
返回一个二元函数,它接受 School *
和 Student *
std :: for_each
期望一个只有 Student *
的一元函子。使用 Boost 。绑定:
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(),
[ & s){DelAndNullify(s);}
);
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));
}
Error 1 error C2064: term does not evaluate to a function taking 1 arguments C:\Program Files\Microsoft Visual Studio 10.0\VC\include\algorithm 22 1 Simulation
Why do I get this error?
updated
change Student
to pStd
updated // algorithm file
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, if I define the DelAndNullify
as static
then the following line passes the compiler
for_each(m_vecStudents.begin(), m_vecStudents.end(), ptr_fun(&School::DelAndNullify));
Updated 05/09/2012
#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;
}
Error 1 error C2535: 'void std::binder1st<_Fn2>::operator ()(Student *&) const' : member function already defined or declared c:\Program Files\Microsoft Visual Studio 10.0\VC\include\xfunctional 299
std::mem_fun(&School::DelAndNullify)
returns a binary functor that takes a School*
and a Student*
, but std::for_each
expects a unary functor taking only a Student*
. Use Boost.Bind instead:
std::for_each(
m_vecStudents.begin(),
m_vecStudents.end(),
boost::bind(&School::DelAndNullify, this, _1)
);
If you have a sufficiently recent compiler then you can use std::bind
or std::tr1::bind
instead of the Boost library; or, if you're using a compiler with C++11 lambda support then you can do the following instead of using any bind
:
std::for_each(
m_vecStudents.begin(),
m_vecStudents.end(),
[this](Student*& s){ DelAndNullify(s); }
);
这篇关于错误C2064:term不评估为带有1个参数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!