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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:59:40+00:00 2026-06-12T13:59:40+00:00

I am having some trouble with the sort function… here is my code: class

  • 0

I am having some trouble with the sort function… here is my code:

class Parola {
public:
    string s;
    int repetition;
    bool operator()(const Parola *x, const Parola *y) {
        return x->repetition > y->repetition;
    }
};


int main(int argc, char** argv) {
    ...
    vector<Parola> p;
    ...
    some insertions here
    ...
    sort(p.begin(), p.end(), Parola());
    ...
    return 0;
}

Why I can’t compile this without errors?
Thanks a lot!

PS: I will show you only the first three lines of over fifty of errors:

/usr/include/c++/4.2.1/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = Parola, _Compare = Parola]':
/usr/include/c++/4.2.1/bits/stl_algo.h:2795:   instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Parola*, std::vector<Parola, std::allocator<Parola> > >, _Size = long int, _Compare = Parola]'
/usr/include/c++/4.2.1/bits/stl_algo.h:2866:   instantiated from 'void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Parola*, std::vector<Parola, std::allocator<Parola> > >, _Compare = Parola]'
  • 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-06-12T13:59:41+00:00Added an answer on June 12, 2026 at 1:59 pm

    Giving the OP some options to choose from: (note: not exhaustive)

    Option 1: Internal operator <()

    class Parola {
    public:
        string s;
        int repetition;
        bool operator<(const Parola& x) const  
        {
            return repetition < x.repetition;
        }
    }
    

    called using the default std::less<> template.

    sort(p.begin(), p.end());
    

    Option 2: Internal Functional operator()():

    class Parola {
    public:
        string s;
        int repetition;
        bool operator()(const Parola& x, const Parola& y) const  
        {
            return x.repetition < y.repetition;
        }
    }
    

    called with optional comparison object, as dasblinken pointed out, odd, but works:

    std::sort(p.begin(), p.end(), Parola());
    

    Option 3: External operator <()

    bool operator <(const Parola& x, const Parola& y)
    {
        x.repetition < y.repetition;
    }
    

    This, like (1), uses the default std::less<> comparator, but requires that the external operator also be a friend of class Parola to have access to the private data members if declared as such. Its use is the same as (1).

    Option 4: External Functor

    class CompareParola
    {
    public:
       bool operator ()(const Parola& x, const Parola& y) const
       {
          return x.repetition < right.repetition;
       }
    };
    

    And used by:

    std::sort(p.begin(), p.end(), CompareParola());
    

    Like (3), the CompareParola class must be friended to Parola if the members being accessed are private:

    Option 5: External Function

    bool ParolaLess(const Parola& x, const Parola& y)
    {
        return x.repetition < y.repetition;
    }
    

    Similar to an external operator or external functional class, this also requires being friended to the object class to gain access to the private members. Invoked like such:

    std::sort(p.begin(), p.end(), ParolaLess);
    

    Option 6: Static Class Function

    class Parola {
    public:
        string s;
        int repetition;
    
        static bool Less(const Parola& x, const Parola& y)  
        {
            return x.repetition < y.repetition;
        }
    };
    

    This is often under-utilized, and has the very nice attribute of having access to all the object member variables, including private ones (obviously, its defined with the class). You can use this by doing:

    std::sort(p.begin(), p.end(), Parola::Less)
    

    Note that this, like (1) and (2), keeps everything in the class.

    Of all of these I prefer (1) for its simplicity, and (4) for its independence, but everyone has their tastes. There are times that (5) or (6) really comes in handy (and I’m a personal fan of (6)).

    If you can think of any more and have the rep to edit it, kindly update this as needed. Please do try to make them at least somewhat useful =P

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble with subroutines, namely the sort function being used to sort
Im having some trouble writing a getstring function, this is what I have so
I'm having some trouble getting a GridView column of strings to sort properly. The
I'm having some trouble converting a parameter: I have this structure: class XMLCO {...};
I am having some trouble populating a large plist into an array. Here is
I'm having some trouble, and getting Command /Developer/Cocotron/1.0/Linux/i386/gcc-4.3.1/bin/i386-ubuntu-linux-gcc failed with exit code 1 in
Having some trouble with R's garbage collection, when passing objects to C++. We have
Having some trouble getting a screenscraper webservice up and running on a weblogic 10.3.3
Having some trouble with what should be a very simple scenario. For example purposes,
Am having some trouble with the SkyDrive download process and hoping you can help

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.