【发布时间】:2011-02-19 18:01:27
【问题描述】:
我有一个函子,我想将它与 sort() 一起使用,有问题的容器具有类型
std::list<std::pair<unsigned, unsigned>>
此容器是在 GameBoard 类的函数之一中临时初始化的。
函子有声明
bool GameBoard::SortMoveList(std::pair<unsigned, unsigned> left,
std::pair<unsigned, unsigned> right)
使用仿函数时出现编译错误,如下所示:
moveList.sort(&GameBoard::SortMoveList);
错误:
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1324): error C2064: term does not evaluate to a function taking 2 arguments
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1394) : see reference to function template instantiation 'void std::list<_Ty>::merge<_Pr3>(std::list<_Ty> &,_Pr3)' being compiled
1> with
1> [
1> _Ty=std::pair<unsigned int,unsigned int>,
1> _Pr3=bool (__thiscall GameBoard::* )(std::pair<unsigned int,unsigned int>,std::pair<unsigned int,unsigned int>)
1> ]
1> GameBoard.cpp(341) : see reference to function template instantiation 'void std::list<_Ty>::sort<bool(__thiscall GameBoard::* )(std::pair<_Ty1,_Ty2>,std::pair<_Ty1,_Ty2>)>(_Pr3)' being compiled
1> with
1> [
1> _Ty=std::pair<unsigned int,unsigned int>,
1> _Ty1=unsigned int,
1> _Ty2=unsigned int,
1> _Pr3=bool (__thiscall GameBoard::* )(std::pair<unsigned int,unsigned int>,std::pair<unsigned int,unsigned int>)
1> ]
知道这里出了什么问题吗? 仿函数需要访问类的私有数据,所以我将它设为成员 fn。如果它不是成员 fn,它编译得很好。我该如何解决这个问题?
谢谢
【问题讨论】:
-
注意,这不是函子,这只是一个函数!
-
你尝试使用排序功能的代码是什么?
-
@Tim 代码是 moveList.sort(&GameBoard::SortMoveList);它列在错误上方的问题中。
-
仿函数是一个类,而不是一个函数。您可以将其设为嵌套类和/或朋友。
-
这是 CS280 作业吗?如果是,则需要标记为作业
标签: c++ visual-studio-2010 stl functor