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

Operators overloading

Hello.
In following code:

class test
{
private:
int t1;
int t2;
public:
test(int i1,int i2){t1=i1;t2=i2;}
bool operator == (const test & ti1, const test & ti2){
if((ti1.t1==ti2.t1)&&(ti1.t2==ti2.t2))return true;return
false;}
};

In gcc 3.3.5 I receive error:

`bool test::operator == (const test&, const test&)' must take
exactly one argument

So how can i compare something if i can't put two operands?
What is wrong with my feeling about it?

PS forgive me my English, its not my native language.

--
empty
Aug 3 '05 #1
3 1437
You invoke "==" over a object of class test.

lets c (lets c++) :

test *a = new test(3,4);
test *b = new test(4,5);

if (*b==*a) // this will invoke == on object pointed to by b
<do something>
endif

opertor overloading would look like

bool operator == (const test & ti1){
if((this->t1==ti1.t1)&&(thist2==*ti1.t2)) //you access
the object on which operator is being invoked using this pointer
return true;

return false;
}

Aug 3 '05 #2
Kuba_O wrote:
In following code:

class test
{
private:
int t1;
int t2;
public:
test(int i1,int i2){t1=i1;t2=i2;}
bool operator == (const test & ti1, const test & ti2){
if((ti1.t1==ti2.t1)&&(ti1.t2==ti2.t2))return true;return
false;}
};

In gcc 3.3.5 I receive error:

`bool test::operator == (const test&, const test&)' must take
exactly one argument

So how can i compare something if i can't put two operands?
What is wrong with my feeling about it?


Two-operand operators defined as non-static members already have the left-
hand side argument declared -- the object (*this), so the right-hand side
is the only _visible_ argument. A two-operand *non-member* operator has
two explicit arguments.

To solve your problem, give your operator == a *single* argument and call
it 'ti'. Inside replace 'ti1' with '(*this)' and 'ti2' with 'ti'.

V
Aug 3 '05 #3

Kuba_O wrote:
Hello.
In following code:

class test
{
private:
int t1;
int t2;
public:
test(int i1,int i2){t1=i1;t2=i2;}
bool operator == (const test & ti1, const test & ti2){
if((ti1.t1==ti2.t1)&&(ti1.t2==ti2.t2))return true;return
false;}
};

In gcc 3.3.5 I receive error:

`bool test::operator == (const test&, const test&)' must take
exactly one argument

So how can i compare something if i can't put two operands?
What is wrong with my feeling about it?

PS forgive me my English, its not my native language.

--
empty


generally, all non-assignment binary operators aren't members of the
class, but instead, are non-member functions. If the function needs
access to the class' privates, then it would be a friend:

class test {
//...
friend bool operator==(const test &ti1, const test &ti2);
};

note that operator== is not a member; it is inside test's class
definition to indicate that operator== is a friend of test.

By making it a non-member function, this allows promotion (google c++'s
explicit keyword) of the left side of operator== in addition to the
right side:

class test {
// ...
test(double);
friend bool operator==(const test &ti1, const test &ti2);
};

/* now: 3.0 == mytest would be allowed in addition to mytest == 3.0
*/

Aug 3 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: denis wendum | last post by:
In a nutshell: What is the equivalent of __radd__ (wich overloads the right hand side of +) when overloading the comparison operators <,>,== and so on. My first guess __rlt__ for overloading the...
6
by: Zenon | last post by:
Folks, I am having a terrible time overloading operators. I have tried what I thought was the correct way, I tried the cheating (friend declarations), all to no avail. Sorry for posting tons of...
2
by: bq | last post by:
Hello, This post is really two questions. Question 1: What is the current status on a revision of ISO C++, specifically regarding plans for overloading composite operators? Some people in this...
20
by: KL | last post by:
I am working on a school assignment, so please don't tell me the solution. I just want some direction. I am supposed to overload the >, <, ==, !=, >=, and <= operators using bool. I am having...
14
by: ambar.shome | last post by:
Hi, As you know there are few operators in C++ which cant be overloaded. They are: .., .*, ::, ?: , new , delete , sizeof , typeid , static_casr , dynamic_cast , const_cast ,...
1
by: Alex Zhitlenok | last post by:
Hi, My question is how to resolve in C# ambiguous overloaded operators? Let say, I have two unrelated classes A and B, each one implements overloaded operator + with the first parameter of type...
10
by: Edward Diener | last post by:
The documentation states the names of the various managed operators but does not give the signature for them. Is there some documentation which I have missed that gives the correct signature ? In...
4
by: Zytan | last post by:
http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/ Does anyone follow this? I know it's good to not have to deal with unsigned types in the interface, so that's great. But...
19
by: Jess | last post by:
Hello, After seeing some examples about operator overloading, I'm still a bit confused about the general syntax. The following is what I think, not sure whether it's correct. 1. For a unary...
3
by: Philipp Brune | last post by:
Hi all, i just played around a little with the c# operator overloading features and an idea came to my mind. You all possibly know the Nullable<T> Datatype. Now i thought it would be a nice idea...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...
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...
0
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...
0
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...

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.