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

operator problem in struct

P: n/a
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::operator<(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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Lars Tackmann wrote:
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::operator<(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.

Jul 22 '05 #2

P: n/a
Lars Tackmann <ro****@diku.dk> wrote in message
news:Pi*******************************@brok.diku.d k...
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::operator<(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.


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.