By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,005 Members | 2,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,005 IT Pros & Developers. It's quick & easy.

sort() function for user defined type

P: n/a
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;
}

Nov 2 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Nov 2 '06 #2

P: n/a

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

Nov 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.