compiler says operator< needs exactly ONE argument (not two--why?)

C

cayblood

Hello, I have the following class declaration in an include file:

class Node
{
public:
Node(string name = "");
Node(const Node& node);
Node& operator=(const Node& node);

bool operator<(const Node& a, const Node& b);
bool operator==(const Node& a, const Node& b);

void add_state(string statename);
void add_child(Node *child);
void add_parent(Node *parent);

void set_probability(Event e, double prob);

double evaluate_marginal(string statename, Event evidence);
double evaluate_markov_blanket(string statename, Event evidence);

private:
static int m_nodecount;

// using vectors on these to preserve ordering information
vector<Node *> m_parents;
vector<Node *> m_children;
vector<string> m_states;

map<Event, double> m_probabilities;
};

When I try to compile my code I get the following error:

include/sbn.h:72: error: 'bool sbn::Node::eek:perator<(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
include/sbn.h:73: error: 'bool sbn::Node::eek:perator==(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
make: *** [bin/sbntest] Error 1

Here is my compiler version information:

powerpc-apple-darwin8-g++-4.0.0 (GCC) 4.0.0 20041026 (Apple Computer,
Inc. build 4061)

Any ideas?

Thanks,

Carl Youngblood
 
C

cayblood

Never mind. Duh... I'm inside the class so I'm obviously comparing the
element passed with the current instance. Sorry to pollute the list.
 
V

Valentin Samko

cayblood said:
class Node
{
public:
bool operator<(const Node& a, const Node& b);
bool operator==(const Node& a, const Node& b);

Don't forget that member functions (operators in this case) already operate on the lhs,
which is "this". So, either change that to
bool operator<(const Node& rhs) const;
bool operator==(const Node& rhs) const;
or make them non member operators, i.e.
friend bool operator<(const Node& a, const Node& b);
friend bool operator==(const Node& a, const Node& b);
if they need an access to protected/private, or just declare them outside of your class
body otherwise.
 
J

John McCabe

cayblood said:
When I try to compile my code I get the following error:
include/sbn.h:72: error: 'bool sbn::Node::eek:perator<(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
include/sbn.h:73: error: 'bool sbn::Node::eek:perator==(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
make: *** [bin/sbntest] Error 1

Here is my compiler version information:

powerpc-apple-darwin8-g++-4.0.0 (GCC) 4.0.0 20041026 (Apple Computer,
Inc. build 4061)

Any ideas?

This is because the other parameter is implicit to some extent. If you
say:

if (a < b)
{
..
..
}

This is essentially the same as:

if (a.operator<(b))
{
..
..
}

See http://www.cplusplus.com/doc/tutorial/classes2.html for more info.
 
I

Ivan Vecerina

: cayblood wrote:
: > class Node
: > {
: > public:
: > bool operator<(const Node& a, const Node& b);
: > bool operator==(const Node& a, const Node& b);
:
: Don't forget that member functions (operators in this case)
: already operate on the lhs,
: which is "this". So, either change that to
: bool operator<(const Node& rhs) const;
: bool operator==(const Node& rhs) const;
: or make them non member operators, i.e.
: friend bool operator<(const Node& a, const Node& b);
: friend bool operator==(const Node& a, const Node& b);
: if they need an access to protected/private,
: or just declare them outside of your class
: body otherwise.

Furthermore, note that the friend/non-member version of
these binary operators should be preferred.
This is because the member version will prevent implicit
conversions from being considered for the lhs argument
(as they are for the rhs argument) - which creates an
ugly asymmetry.

For instance, when using:
class Real
{
/* ... */
Real( double d );
/* ... */

//VERSION A:
bool operator == ( Real const& rhs ) const;
//VERSION B:
friend bool operator == ( Real const& lhs, Real const& rhs);

/* ... */
};

void foo()
{
Real r(5.0);
bool a = r == 8; // ok with either version of op<0
bool b = 8 == r; // only compiles with VERSION B
//Who would want the 2nd line to fail if the 1st is ok???
}

The asymmetry associated with Version A (the non-friend
member function) is undesirable.


hth -Ivan
 

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,011
Latest member
AjaUqq1950

Latest Threads

Top