sort() function for user defined type

Discussion in 'C++' started by alice, Nov 2, 2006.

  1. alice

    alice Guest

    When I'm trying to compile the below program on the GPP compiler, it is
    giving me errors like
    " no match for operator < ..."

    Can anybody please help me figure out the error?


    #include <iostream>
    #include <string>
    #include <algorithm>

    using namespace std;

    typedef struct node
    {
    int i;
    int j;
    bool operator<(struct node& n1)
    {
    return (*this.i < n1.i);
    }
    }node;



    int main(void)
    {
    node data[10];
    sort(data,data+10);
    return 0;
    }
    alice, Nov 2, 2006
    #1
    1. Advertising

  2. alice wrote:
    > When I'm trying to compile the below program on the GPP compiler, it
    > is giving me errors like
    > " no match for operator < ..."


    Take a habit not to use '...' in your posts unless absolutely necessary.

    >
    > Can anybody please help me figure out the error?
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <algorithm>
    >
    > using namespace std;
    >
    > typedef struct node


    Drop this C habit. Should just be

    struct node

    > {
    > int i;
    > int j;
    > bool operator<(struct node& n1)


    bool operator<(node const& n1) const

    > {
    > return (*this.i < n1.i);
    > }
    > }node;


    And remove it here. Should just be

    };

    >
    >
    >
    > int main(void)
    > {
    > node data[10];
    > sort(data,data+10);
    > return 0;
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 2, 2006
    #2
    1. Advertising

  3. Re: sort() function for user defined type

    Victor Bazarov wrote:

    You missed one.

    > > #include <iostream>
    > > #include <string>
    > > #include <algorithm>
    > >
    > > using namespace std;
    > >
    > > typedef struct node

    >
    > Drop this C habit. Should just be
    >
    > struct node
    >
    > > {
    > > int i;
    > > int j;
    > > bool operator<(struct node& n1)

    >
    > bool operator<(node const& n1) const
    >
    > > {
    > > return (*this.i < n1.i);


    The above line should be:

    return (i < n1.i);

    or if you insist on keeping the "this" (Why?), then it should be:

    return (this->i < n1.i);

    Best regards,

    Tom
    Thomas Tutone, Nov 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oodini
    Replies:
    1
    Views:
    1,738
    Keith Thompson
    Sep 27, 2005
  2. Replies:
    1
    Views:
    439
    Sylvester Hesp
    May 16, 2007
  3. Replies:
    3
    Views:
    500
    Kai-Uwe Bux
    Jul 17, 2007
  4. Navin
    Replies:
    1
    Views:
    661
    Ken Schaefer
    Sep 9, 2003
  5. Alok
    Replies:
    3
    Views:
    233
Loading...

Share This Page