开发者

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 Del开发者_如何学编程AndNullify(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); }
);


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.


this

mem_fun(&School::DelAndNullify)

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

Use

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

instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜