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 6892365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:31:33+00:00 2026-05-27T06:31:33+00:00

Class: Class: private: … vector<string> words; vector< list<int> > vints; public: myFunction(…) I am

  • 0

Class:

Class:
  private:
    ...
    vector<string> words; 
    vector< list<int> > vints;
  public:
    myFunction(...)

I am calling a sort on non-empty list in another member function:

void myClass::myFunction (...) {
    ...
    if (!vints[i].empty()) vints[i].sort(sortFunc);
    ...
}

My sorting function:

bool myClass::sortFunc(const int& i, const int& j) { return (words[i] < words[j]); }

The Error:

error: no matching function for call to ‘std::list<int, std::allocator<int>      >::sort(<unresolved overloaded function type>)’
/usr/include/c++/4.4/bits/list.tcc:301: note: candidates are: void std::list<_Tp,     _Alloc>::sort() [with _Tp = int, _Alloc = std::allocator<int>]
/usr/include/c++/4.4/bits/list.tcc:378: note:                 void std::list<_Tp, _    Alloc>::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (SuperWordSearch::*)    (const int&, const int&), _Tp = int, _Alloc = std::allocator<int>]

I have researched and come across the following questions:

C++ Custom compare function for list::sort

Problem sorting a list of pointers

Error in std::list::sort with custom comparator (expected primary-expression before ')' token)

and they would have been sufficient had it not been for the fact that in this class, the sortFunc depends on the member variable WORDS for that instance of the object. So I cannot make the comparator function (sortFunc) static or global

EDIT: Just came across this How to sort a std:list when you need member data? and it provides a solution by making a friend class, but is it possible to accomplish this inside the user-defined class itself?

  • 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-27T06:31:34+00:00Added an answer on May 27, 2026 at 6:31 am

    @Kerrek’s answer involving lambdas is better. But, if you must avoid C++11 features, then replace your sort function with a functor. Allow that functor to store a reference to whatever data is required, as so:

    #include <vector>
    #include <list>
    #include <string>
    
    class myClass {
    private:
      std::vector<std::string> words;
      std::vector<std::list<int> > vints;
    
      // Instead of sortFunc, use sortFunctor. A functor can be used in place 
      // of a function in many places, and it can carry state (like a reference
      // to the data it needs).
      struct sortFunctor {
        const std::vector<std::string>& words;
        sortFunctor(const std::vector<std::string>& words) : words(words) { }
        bool operator()(int i, int j) { return words[i] < words[j]; }
      };
    
    public:
      void myFunction() {
        vints[0].sort(sortFunctor(words));
      }
      myClass() {
        words.push_back("apple");
        words.push_back("berry");
        std::list<int> l;
        l.push_back(0);
        l.push_back(1);
        vints.push_back(l);
      }
    };
    
    int main () {
      myClass object;
      object.myFunction();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class: private class Info{ public String A; public int B;
I have this class: private static class ClassA{ int id; String name; public ClassA(int
class String { private: char* rep; public: String (const char*); void toUpper() const; };
private class CompAdvertisements : IComparer<Advertisements> { private string OrderBy { get; set; } public
public class Ex7 { private int fld; private void meth(int val) { fld =
I created the following within my class private List<CreditCardTransaction> _ccTransactions = new List<CreditCardTransaction>(); public
If I have a class as follows class Example_Class { private: int x; int
A little example TTest<T> = class private f : T; public function ToString :
class B{ private: void DoSomething(); } class W{ private: class D: public B{ }
class ConfigReader { private static $instance = NULL; protected $configData = array(); public function

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.