Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3967704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:45:07+00:00 2026-05-20T03:45:07+00:00

I have a functor which i want to use with sort() the container in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:45:07+00:00Added an answer on May 20, 2026 at 3:45 am

    A functor is a an object that behaves like a function.

    This means you need to define a class that defines the operator()

    Example:

    class GameBoardMoveListSorter
    {
        bool operator()(std::pair<unsigned, unsigned> const& left, 
                        std::pair<unsigned, unsigned> const& right) const
        {
            return left.first < right.first; // or whatever your strict weak ordering is.
        }
    };
    
    /// STUFF
    
    moveList.sort(GameBoardMoveListSorter());
    

    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

    Like a member function, a friend function (11.4) defined within a nested class is in the lexical scope of that class; it obeys the same rules for name binding as a static member function of that class (9.4), but it has no special access rights to members of an enclosing class.

    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.

    #include <memory>
    
    class Chess
    {
        private:
            int     board[8][8];
    
    
            class GameBoardMoveListSorter
            {
                GameBoardMoveListSorter(Chess& p)
                    : parent(p)
                {}
    
                bool operator()(std::pair<unsigned, unsigned> const& left,
                                std::pair<unsigned, unsigned> const& right) const
                {
                    int val = parent.board[0][0] + parent.board[7][7];
                    return left.first + val < right.first - val; // or whatever your strict weak ordering is.
                }
    
                Chess&      parent;
            };
            // I believe that it must be a friend to access private members.
            friend class GameBoardMoveListSorter;
    
        public:
            void makeMove()
            {
                 std::list<std::pair<unsigned, unsigned> >  moveList(/*Generate Moves*/);
    
                 moveList.sort(GameBoardMoveListSorter(*this));
                 // Do something with the move list.
            }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a container like this: // Sort functor struct SortByTime : std::binary_function<const TimeSortableData
I have the following situation: I have a object of type MyClass , which
Given a functor appropriate for use with std::for_each and friends: template <typename T> struct
I have found this example on StackOverflow: var people = new List<Person> { new
Suppose i want something simple like the following: I have an core-algorithm, which randomly
I have something like a sort-algorithm here, and I want to pass it a
I have a template function which accepts a function-object ('functor') as a template parameter:
Hey I have an abstract class named Partition which is a functor, and its
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you managed to get Aptana Studio debugging to work? I tried following this,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.