473,387 Members | 1,504 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,387 software developers and data experts.

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

Similar topics

14
by: mjkahn | last post by:
I've read (and read!) that you shouldn't store objects in Session variables. I've read these reasons: - The object takes up memory that may not be freed until the session times out. Better to...
0
by: Bobbak | last post by:
Hello All, I could really use some help with this bit of code I am working on, every time I come to execute it I get an error that says "Compile Error: Argument not optional". Now I am using...
14
by: portroe | last post by:
anybody have an idea on what i am doing wrong here, Argument not specified for parameter 'JobTitle' of 'Public Sub New(JobTitle As String, NumberOfEmployees As Integer)'. thanks Portroe
3
by: ericw3 | last post by:
I am switching from Java to the .Net framework. In order to separate page layout design from the application code, I studied the codebehind tutorial by John at...
2
by: Gilz | last post by:
Hi I have an after update event on a field on my people form. When i try to debug the code i get an "Argument not optional" error in relation to the IIf statement. I used the same code in...
7
by: Tony Girgenti | last post by:
Hello. I'm developing and testing a web application using VS.NET 2003, VB, .NET Framework 1.1.4322, ASP.NET 1.1.4322 and IIS5.1 on a WIN XP Pro, SP2 computer. I'm using a web form. I'm...
2
by: manojpolawar | last post by:
anybody have an idea on what i am doing wrong here, Argument not specified for parameter 'JobTitle' of 'Public Sub New(JobTitle As String, NumberOfEmployees As Integer)'.
2
by: Kenneth Porter | last post by:
I try to be const correct everywhere and found that I can't pass a const String to this API. Is it going to change the value? Or is the signature not the best it could be? I want to write this:...
3
by: rnashting | last post by:
Good Morning! I am still very new to VBA, and I'm getting an error that I can't figure out with a basic code. Here's what I have at this point: Option Compare Database Option Explicit ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.