Error C2064: the term does not evaluate a function that takes 1 argument

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: the term does not evaluate a function that takes 1 argument C: \ Program Files \ Microsoft Visual Studio 10.0 \ VC \ include \ algorithm 22 1 Modeling

Why am I getting this error?


updated

change StudenttopStd


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

By the way, if I define DelAndNullifyas static, then the next line passes the compiler

for_each(m_vecStudents.begin(), m_vecStudents.end(), ptr_fun(&School::DelAndNullify));

Updated 09/05/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 is already defined or declared c: \ Program Files \ Microsoft Visual Studio 10.0 \ VC \ include \ xfunctional 299

+3
source share
3 answers

std::mem_fun(&School::DelAndNullify) , School* a Student*, std::for_each , a Student*. Boost. Bind:

std::for_each(
    m_vecStudents.begin(),
    m_vecStudents.end(),
    boost::bind(&School::DelAndNullify, this, _1)
);

, std::bind std::tr1::bind Boost; , lambda ++ 11, bind:

std::for_each(
    m_vecStudents.begin(),
    m_vecStudents.end(),
    [this](Student*& s){ DelAndNullify(s); }
);
+6

, mem_fun - "" , , :

static void DelAndNullfify(Student *pStudent);

pre-mem_fun'd, :

static void DelAndNullfify(School *pSchool, Student* &prStudent);

.

+1

mem_fun(&School::DelAndNullify)

, School* Student*.

bind1st(mem_fun(&School::DelAndNullify), this)

.

+1

All Articles