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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:25:16+00:00 2026-05-21T20:25:16+00:00

i have a class like this: class test { vector<expr*> _exprs; bool cmp(expr* e1,

  • 0

i have a class like this:

class test
{  
    vector<expr*> _exprs;  

    bool cmp(expr* e1, expr* e2);   

    ExprManager* mg;  
}  

and the Comparison function is:

bool test::cmp(expr* e1, expr* e2)  
{  
    return exprHight(mg, e1) < exprHight(mg, e2);  
}  

then when i use the comparison function in sorting the _exprs(in another member function)

sort(_exprs->begin(), _exprs->end(), cmp);  

The compiler report:

error: argument of type ‘bool (test::)(void*, void*)’ does not match ‘bool (test::*)(void*, void*)’

How to fix it? Thanks for advance.

  • 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-21T20:25:17+00:00Added an answer on May 21, 2026 at 8:25 pm

    The normal way to define a predicate for sorting is as a functor.

    Make your class define the operator() to do the comparing, and you can just pass an instance of the class to std::sort.

    So really, all you need to do is to define the class like this instead:

    class test
    {  
        vector<expr*> _exprs;  
    
        bool operator()(expr* e1, expr* e2);   
    
        ExprManager* mg;  
    }  
    

    And then you can call sort like this:

    sort(_exprs->begin(), _exprs->end(), test());  
    

    or, of course, use an existing instance of the test class, instead of constructing a new one. But you just pass in a class instance, and don’t need to mention the member function at all.

    If the sorting takes place in another member function (it looks that way from your reference to _exprs), write

    sort(_exprs->begin(), _exprs->end(), *this);  
    

    One thing to note is that std::sort, like most other standard library algorithms, copies the predicate object, and so your predicate class must be able to handle copying safely (which your classes should always do anyway)

    In short, the way to achieve this is to follow the “rule of three”.

    If your class defines a destructor, copy constructor or assignment operator, then it should almost certainly define all three.

    If the compiler-generated copy constructor is used, it will simply copy your class’ pointer members, so you will have two objects containing pointers to the same object.

    If the class has a destructor which calls delete on that pointer, then that ends up being done twice. Which is an error.

    If your class is intended to be copyable (and most of the standard library requires this), then you must define appropriate copy constructors and assignment operators to implement it safely (for example, copy the resource pointed to by the pointer, or use a smart pointer instead).

    IF your class is not intended to be copyable, then you should define the copy constructor and assignment operators to be private, so that attempts to copy the class will result in a compile-time error, instead of runtime crashes.

    You should never ever define a class which can be copied, but doesn’t do so correctly. Either define the necessary copy constructor/assignment operator/destructors to handle copying, or make copying impossible by making copy ctor/assignment operator private.

    Wrapping pointers to dynamically allocated memory owned by the class in smart pointers is a simple way to get copyability for free.

    If the class contains only RAII objects, then you don’t need to define a destructor at all, and so the rule of three doesn’t require you to define copy constructor and assignment operator either.

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

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have a test sample about coherence lock-unlock mechanism like this: public class Test
I have a class like this: public class test { public virtual int Id
Suppose I have a Java class like this: public class Test { static {
I have a class that goes like this: function Element(){ this.changes = {}; }
Say I have code like this: class Car def test_drive!; end end class AssemblyLine
I have class like this below shown. which contains the shopping items where the
I have a class like this - class Messages { ... LinkedList<String> inputs; LinkedList<String>
I have a class like this: abstract class CrudResource extends Controller { type ResourceIdType
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <

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.