I have a functor which i want to use with sort() the container in question has the type
std::list<std::pair<unsigned, unsigned>>
This container is a temporary initialized in one of class GameBoard’s functions.
the functor has the declaration
bool GameBoard::SortMoveList(std::pair<unsigned, unsigned> left,
std::pair<unsigned, unsigned> right)
I get the compile error when i use the functor as follows:
moveList.sort(&GameBoard::SortMoveList);
Error:
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> ]
Any idea whats going wrong here?
The functor needs to access the class’s private data, so i made it a member fn. If its not a member fn, it compiles fine. How can I solve this problem?
Thanks
A functor is a an object that behaves like a function.
This means you need to define a class that defines the operator()
Example:
Edit Based on Comment:
Comments from others please:
I though the new standard gave inner classes accesses to the enclosing classes private members. But having just re-read the standard that does not seem to be the wording I am seeing (the behavior of the compiler seems to allow accesses (though I know the conformance in this area has always been weak)).
Section 9.7 Paragraph 4
Based on the above section of the manual. The inner class must be a friend class to accesses the private members of the outer class.
Note. Unlike java there is no implied parent relationship between inner and outer class. Thus the inner class must have an explicit reference to an outer class object to access its members.