gpt4 book ai didi

c++ - 错误 C2064 : term does not evaluate to a function taking 1 arguments

转载 作者:太空狗 更新时间:2023-10-29 20:06:48 31 4
gpt4 key购买 nike

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

为什么会出现此错误?


已更新

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(), m_vecStudents.end(), 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(), 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) 返回一个带有 School*Student* 的二元仿函数,但是 std::for_each 期望一元仿函数只采用 Student*。使用 Boost . Bind相反:

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

如果您有足够新的编译器,那么您可以使用 std::bindstd::tr1::bind 而不是 Boost 库;或者,如果您使用的是支持 C++11 lambda 的编译器,那么您可以执行以下操作而不是使用任何 bind:

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

关于c++ - 错误 C2064 : term does not evaluate to a function taking 1 arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6065041/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com