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

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

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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.