473,624 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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==(cons t Node& a, const Node& b);

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

void set_probability (Event e, double prob);

double evaluate_margin al(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::oper ator<(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
include/sbn.h:73: error: 'bool sbn::Node::oper ator==(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 33429
cayblood wrote:
class Node
{
public:
bool operator<(const Node& a, const Node& b);
bool operator==(cons t 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==(cons t Node& rhs) const;
or make them non member operators, i.e.
friend bool operator<(const Node& a, const Node& b);
friend bool operator==(cons t 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::oper ator<(const sbn::Node&,
const sbn::Node&)' must take exactly one argument
include/sbn.h:73: error: 'bool sbn::Node::oper ator==(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.r eadfreenews.net ...
: cayblood wrote:
: > class Node
: > {
: > public:
: > bool operator<(const Node& a, const Node& b);
: > bool operator==(cons t 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==(cons t Node& rhs) const;
: or make them non member operators, i.e.
: friend bool operator<(const Node& a, const Node& b);
: friend bool operator==(cons t 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
2593
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 create the object only when you actually use it. - Causes poor performance because the thread that created the object has to service all requests for it. Assuming I can live with the memory and performance implications (a big if,
0
4412
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 Access 2002 to run this code that will allow me to populate a field in several tables at once. Here is an example of the code. Private Sub cmdOK_Click() DoCmd.SetWarnings False
14
14465
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
1996
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 http://www.asp101.com/articles/john/codebehindnovs/default.asp In my GetQueryResult.aspx, I defined a DataGrid as follows: <asp:DataGrid ID ="dgQueryResults" runat="server" AllowPaging="True"
2
4345
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 an update query and it worked perfectly. PE_Initials = (Left(, 1) & IIf(InStr(1, ,
7
11460
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 trying create a function in javascript to compare dates between calendar selected dates(that's not really my problem yet). I can't get the html to recognize the function that i have created. When i
2
2013
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
2302
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: // based on http://msdn.microsoft.com/en-us/magazine/bb985936.aspx std::string MarshalString (const System::String^ s) {
3
10070
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 Private Sub cmdLarger_Click() FindLargestNumber End Sub Public Function FindLargestNumber(ByVal Number1 As Double, ByVal Number2 As Double)
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8629
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.