sort() function for user defined type

A

alice

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;
}
 
V

Victor Bazarov

alice said:
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
 
T

Thomas Tutone

Victor Bazarov wrote:

You missed one.
Drop this C habit. Should just be

struct node


bool operator<(node const& n1) const

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top