3
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

q0987
  • 34,938
  • 69
  • 242
  • 387

3 Answers3

6

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); }
);
ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • you have `std::bind1st` in standard C++ (1998) which works well here. No need for boost. – Alexandre C. May 19 '11 at 21:36
  • @AlexandreC : Technically agreed, but I never recommend the C++98 functional adapters to anyone as their usage is so ugly and verbose. – ildjarn May 19 '11 at 21:37
  • @ildjam: in this simple case, setting up boost and having your compilation times skyrocket is not worth it. However, you're right in that the OP can use `std::bind` since (s)he uses VS2010. – Alexandre C. May 19 '11 at 21:38
  • @AlexandreC : I'd be amazed if including Boost.Bind affects compile times at all (especially with precompiled headers enabled), and it's always worth setting up Boost for non-trivial C++ work IMO. – ildjarn May 19 '11 at 21:47
1

Looks like mem_fun turns your member function into a "static" function, which takes an object as its first parameter, like:

static void DelAndNullfify(Student *pStudent);

But you already have a parameter in the pre-mem_fun'd function, so you end up with:

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

That's one parameter too much.

Andomar
  • 232,371
  • 49
  • 380
  • 404
1

this

mem_fun(&School::DelAndNullify)

returns a binary function, expecting a School* and a Student*.

Use

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

instead.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • I have tried, but it still doesn't work. 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 1 Simulation – q0987 May 19 '11 at 21:42
  • @q087: Did you notice I forgot a colon ? Fixed now, should work. – Alexandre C. May 20 '11 at 06:59
  • I have posted all my sample code that has adopted your suggestion. However, the error still holds. May you take a try? -- thank you – q0987 May 22 '11 at 14:13
  • @q0987: You're missing `#include `. – Alexandre C. May 22 '11 at 15:10
  • @q0987: also, `using namespace std` is often a bad idea (especially with MSVC). If it doesn't work, try to qualify: `std::bind1st(std::mem_fun(...))` – Alexandre C. May 22 '11 at 15:12