错误C2064:术语不等于带有1个参数的函数

问题描述

||
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:术语未评估为带有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);
}
顺便说一句,如果我将
DelAndNullify
定义为
static
,则以下行通过了编译器
for_each(m_vecStudents.begin(),ptr_fun(&School::DelAndNullify));
更新于2012年5月9日
#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(),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 <_Fn2> :: operator()(学生   *&)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.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(),[this](Student*& s){ DelAndNullify(s); }
);
    ,看起来像
mem_fun
将您的成员函数变成一个\“ static \”函数,该函数将对象作为其第一个参数,例如:
static void DelAndNullfify(Student *pStudent);
但是您在pre-mem_fun \'d函数中已经有一个参数,因此最终得到:
static void DelAndNullfify(School *pSchool,Student* &prStudent);
那是一个太多的参数。     ,这个
mem_fun(&School::DelAndNullify)
返回一个二进制函数,期望为9和10。 采用
bind1st(mem_fun(&School::DelAndNullify),this)
代替。