473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

operator problem in struct

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
2 2385
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
14
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
6
by: c++newbie | last post by:
Hi all, I try to compile the following classes: main.cpp: #include <algorithm> #include <iostream> #include <fstream> #include <iterator>
3
by: MojoRison | last post by:
struct A{ A& operator=( const A& a ){ i = a.i; return *this; } private: int i; }; struct B : public A{ B& operator=( const B& b ){ j = b.j; return *this; } // how to copy i?
14
by: indigodfw | last post by:
Greetings from India I would like to know the rationale on allowing structs to be assigned (using = operator) and not allowing equality operator ( == operator) on them. The compiler when it...
10
by: Adrian | last post by:
Below is an example of the problem I am having. I don't understand how I can get the compiler to see deriv &operator=(const T &rhs). I am sure this is a common problem - any suggestions? ...
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
19
by: Rajesh S R | last post by:
Consider the following code: int main() { struct name { long a; int b; long c; }s={3,4,5},*p; p=&s;
7
by: MWimmer | last post by:
Dear members of this group, recently I came across a problem with repsect to operator=() and inheritance. Consider the following code snippet: ...
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.