Sort: Error No matching function

This is a discussion on Sort: Error No matching function within the C++ Programming forums, part of the General Programming Boards category; I have a error with sort algorithm of c++: Code: struct point{ //define point } bool cmp(point a, point b){ ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Sort: Error No matching function

    I have a error with sort algorithm of c++:
    Code:
    struct point{ //define point
    }
    bool cmp(point a, point b){ //some line of code
    }
    typedef vector<point> polygon;
    polygon Convex_hull(polygon&p){
    sort(p.begin,p.end,cmp); //error here: NO MATCHING FUNCTION
    I don't know where my mistake is. Help me please.
    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    This:
    Code:
    sort(p.begin,p.end,cmp);
    should be:
    Code:
    sort(p.begin(), p.end(), cmp);
    Incidentally, if you only have one way to compare point objects for a "less than" comparison, then it may make sense to overload operator< instead of defining this cmp function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    Oh, thanks so much, I have use define and forget '()'
    Code:
    #define ALL(a) (a).begin(),(a).end  //WRONG HERE
    Yes, I'm trying to use overloading because my comparison need more parameters. (above code just my test to find why it fall).
    But after I used that, I have meet error:
    Code:
    struct cmp_hull{
           point pivot; //this is a third parameter for my comparison so I must use struct to replace cmp function above
           bool operator()(point p, point q){ //someline of code to comparison)
           }
    };
    polygon Convex_hull(polygon&p){
           cmp_hull comp;   //ERROR: synthesized method cmp_hull::cmp_hull() first request here.
    //...
            sort(ALL(p),comp);
    }
    Maybe some problems with my code again I try to use advanced feature of class/struct and meet so many error. Can you help me, please
    thanks
    Last edited by hqt; 12-31-2011 at 10:46 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    I should have complained earlier, grr... please indent your code properly.

    Also, it helps if you provide the smallest and simplest program that you expect would compile but which demonstrates the compile error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Oh, I'm sorry so much.
    I have indented my code again. I'm sorry because I type above code in web field, so use 'tab' is so difficult I still cannot paste my code in my IDE to browser to, because it's always different computer. (my computer that I use to program not have network )
    But next time, I will make my code clearly.
    thanks

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Once again, it helps if you provide the smallest and simplest program that you expect would compile but which demonstrates the compile error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    this is my code with some shorten (struct point just make my program don't have error, you might not see that struct . the program I meet from struct comp_hull and function convex_hull_grahamscan()
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<vector>
    #include<algorithm>
    using namespace std;
    #define FOR(i,a,b) for(int i=(a);i<(b);++i)
    #define REP(i,n) FOR((i,0,n)
    #define ALL(a) (a).begin,(a).(end)
    #define SZ(a) ((int)a.size())
    
    inline int cmp(int a,int b){return (a<=b)?(a<b)?-1:0:1;}
    struct point{
        int x,y;
        point(){x=y=0;}
        point(int x, int y):x(x),y(y){}
        point operator+(point q){return point(x+q.x,y+q.y);}
        point operator-(point q){return point(x-q.x,y-q.y);}
        int operator*(point q){return x*q.x+y*q.y;}
        int operator%(point q){return x*q.y-y*q.x;}
    };
    inline int ccw(point a, point b, point c){return cmp((a-b)%(c-b),0);}
    typedef vector<point> polygon;
    struct cmp_hull{
        point pivot;
        bool operator()(point p, point q)
        {
            int R=ccw(p,pivot,q);
            if(R) return R<0;
            int a=(p-pivot)*(p-pivot), b=(q-pivot)*(q-pivot);
            return cmp(a,b);
        }
    };
    polygon Convex_Hull_GrahamScan(polygon&p)
    {
        int j=0; polygon r(SZ(p));
        if(SZ(p)) return r;
        cmp_hull comp; //ERROR HERE AS I SAY BEFORE
        //synthesized method 'cmp_hull::cmp_hull()' first required here |
        //....
    }
    I found that, if I close IDE and open again, my program will run (although maybe something don't right). but if I change something, this error come again and I restart IDE and....

    Help me please this problem make me headache.
    thanks for ton
    Last edited by hqt; 12-31-2011 at 11:46 PM.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    sort requires that it's function argument return a Boolean: true, if the first argument to the function comes before the second.

    Are you from C? It's not like qsort.

  9. #9
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    As the latest code that I have posted, it has error on line declare:
    Code:
     cmp_hull comp;
    not because sort function.
    I have typed again and still met that error I really don't know why.
    Please help me explain this, thanks

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,740
    Try and create an empty default constructor for your structure.
    Code:
    struct cmp_hull{
        cmp_hull() {}  // add this line.
        point pivot;
        bool operator()(point p, point q)
        {
            int R=ccw(p,pivot,q);
            if(R) return R<0;
            int a=(p-pivot)*(p-pivot), b=(q-pivot)*(q-pivot);
            return cmp(a,b);
        }
    };

    Jim

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    Quote Originally Posted by hqt View Post
    As the latest code that I have posted, it has error on line declare:
    Code:
     cmp_hull comp;
    not because sort function.
    I have typed again and still met that error I really don't know why.
    Please help me explain this, thanks
    You still should not write the cmp function the way that you have.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    What whiteflags is telling you is that the cmp funtion would instead need to be defined like this:
    Code:
    bool cmp(int a, int b) { return a<b; }
    However, as you can see the definition of it is so trivial that you can just use the less-than operator directly and remove the function altogether.

    You also probably want a non-default constructor:
    Code:
    struct cmp_hull{
        cmp_hull(point pivot) : pivot(pivot) {}  // add this line.
        point pivot;
    This allows you to easily pass in the pivot when constructing the cmp_hull object:
    Code:
    point my_pivot(some_x, some_y);
    
    cmp_hull comp(my_pivot);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Compared objects should also be passed into your function as const references. The member function should itself be const as well:
    Code:
    struct cmp_hull{
        ...
        bool operator()(const point& p, const point& q) const
        {
            ...
        }
    };
    When your code is compiled and it sees you trying to compare objects like this, compilers will search for an appropriate function that is const rather than non-const and has the compared objects being passed as const. If none is found they will sometimes complain that no matching function exists. Logically, why would a comparison test affect the state of the objects... so, why would the objects need to passed as anything other than const?

    [edit]
    Incidentally, your belief that you need a struct because of the "third parameter" requirement is not true. An overloaded less-than operator can have an internal point object just like the one your struct makes use of.
    [/edit]
    Last edited by hk_mp5kpdw; 01-03-2012 at 05:53 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  2. No Matching Function Call for Constructor
    By manasij7479 in forum C++ Programming
    Replies: 5
    Last Post: 02-07-2011, 02:29 PM
  3. Overloaded function parameter matching
    By kmdv in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2010, 09:28 AM
  4. No Matching Function Error When Opening File
    By zephyrcat in forum C++ Programming
    Replies: 19
    Last Post: 07-15-2008, 01:36 PM
  5. no matching function for call to
    By f6ff in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2006, 03:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21