469,636 Members | 1,555 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

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

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::operator<(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
include/sbn.h:73: error: 'bool sbn::Node::operator==(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

Nov 3 '05 #1
4 30967
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.

--

Valentin Samko - http://www.valentinsamko.com
Nov 3 '05 #2
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.

Nov 3 '05 #3
"cayblood" <ca*************@gmail.com> wrote:
When I try to compile my code I get the following error: include/sbn.h:72: error: 'bool sbn::Node::operator<(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
include/sbn.h:73: error: 'bool sbn::Node::operator==(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.

Nov 3 '05 #4
"Valentin Samko" <c+***********@digiways.com> wrote in message
news:43***********************@authen.white.readfr eenews.net...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by portroe | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.