Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sort() function for user defined type

Reply
Thread Tools

sort() function for user defined type

 
 
alice
Guest
Posts: n/a
 
      11-02-2006
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-02-2006
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


 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      11-02-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mapping betwing a standard type and a user defined type with SWIG Lyes Amazouz Ruby 2 08-20-2008 04:01 PM
Type conversion function for user defined type... zaeminkr@gmail.com C++ 1 05-16-2007 09:00 AM
casting primitive type to user-defined type works in usage xllx.relient.xllx@gmail.com C++ 2 04-15-2006 05:37 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) Oodini C Programming 1 09-27-2005 07:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57