operator problem in struct

L

Lars Tackmann

Hi i have a struct defined - and would like to define the usual comparison
opaerators on it (<=, == ...). My struct look like this:

--------------------------------------------------------------
typedef struct Node{
double value; // value of node
char *name; // name of node

// define I/O on nodes
friend ostream &operator<<(ostream &out, const Node &n){
out << n.name << " " << n.value;
return out;
}
friend istream &operator>>(istream &in, Node &n){
in >> n.name >> n.value;
return in;
}

// define comparison operators on nodes
bool operator<(const Node &rNode, const Node &lNode){
return bool(rNode.value < lNode.value);
}
} Node;
------------------------------------------------------------------

if i try to compile this code(with g++ 3.2.2) i get

------------------------------------------------------------------
bool Node::eek:perator<(const Node&, const Node&)' must take exactly
one argument
------------------------------------------------------------------

Which does seam a bit odd because the < operator is infix and takes two
arguments. So my question is how do i implment my struct i norder to be
able to define comparison on it ????.


Thanks
 
D

Deming He

Lars Tackmann said:
Hi i have a struct defined - and would like to define the usual comparison
opaerators on it (<=, == ...). My struct look like this:

--------------------------------------------------------------
typedef struct Node{
double value; // value of node
char *name; // name of node

// define I/O on nodes
friend ostream &operator<<(ostream &out, const Node &n){
out << n.name << " " << n.value;
return out;
}
friend istream &operator>>(istream &in, Node &n){
in >> n.name >> n.value;
return in;
}

// define comparison operators on nodes
bool operator<(const Node &rNode, const Node &lNode){
return bool(rNode.value < lNode.value);
}
} Node;
------------------------------------------------------------------

if i try to compile this code(with g++ 3.2.2) i get

------------------------------------------------------------------
bool Node::eek:perator<(const Node&, const Node&)' must take exactly
one argument
------------------------------------------------------------------

Which does seam a bit odd because the < operator is infix and takes two
arguments. So my question is how do i implment my struct i norder to be
able to define comparison on it ????.


Thanks

You missed last "friend" in front "bool operator<..."

There's an implicit one operand already for "<" operator. If you overload
it, make it a friend function so that it takes 2 arguments as you defined.
 
R

Rolf Magnus

Lars said:
Hi i have a struct defined - and would like to define the usual
comparison opaerators on it (<=, == ...). My struct look like this:

--------------------------------------------------------------
typedef struct Node{
double value; // value of node
char *name; // name of node

// define I/O on nodes
friend ostream &operator<<(ostream &out, const Node &n){
out << n.name << " " << n.value;
return out;
}
friend istream &operator>>(istream &in, Node &n){
in >> n.name >> n.value;
return in;
}

// define comparison operators on nodes
bool operator<(const Node &rNode, const Node &lNode){
return bool(rNode.value < lNode.value);

You don't actually need the cast here. The value of

rNode.value < lNode.value

is already of type bool.

}
} Node;
------------------------------------------------------------------

if i try to compile this code(with g++ 3.2.2) i get

------------------------------------------------------------------
bool Node::eek:perator<(const Node&, const Node&)' must take exactly
one argument
------------------------------------------------------------------

Which does seam a bit odd because the < operator is infix and takes
two arguments. So my question is how do i implment my struct i norder
to be able to define comparison on it ????.

Just the same, but give the operator only one argument. The 'this'
object is the left hand argument of the operator.
 

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
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top