Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   sort() function for user defined type (http://www.velocityreviews.com/forums/t458113-sort-function-for-user-defined-type.html)

alice 11-02-2006 06:58 PM

sort() function for user defined type
 
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;
}


Victor Bazarov 11-02-2006 07:00 PM

Re: sort() function for user defined type
 
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



Thomas Tutone 11-02-2006 07:07 PM

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



All times are GMT. The time now is 04:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.