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

Operators overloading

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


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

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.