Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Overloading the "=" operator for Complex numbers

Reply
Thread Tools

Overloading the "=" operator for Complex numbers

 
 
Pmb
Guest
Posts: n/a
 
      05-29-2004
I've been working on creating a Complex class for my own learning purpose
(learn through doing etc.). I'm once again puzzled about something. I can't
figure out how to overload the assignment operator.

Here's what I'm trying to do. I've defined class Complex as

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex operator=( Complex & );
Complex conjugate();
float magnitude();
private:
float re;
float im;
};

The constructor is

Complex::Complex( float a, float b )
: re( a ), im( b ) { }

I have no idea how to write the overload function though. This doesn't work

Complex Complex:perator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}


Help!!!!

Thanks in advance!

Pmb


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-29-2004
* "Pmb" <> schriebt:
> I have no idea how to write the overload function though. This doesn't work
>
> Complex Complex:perator=( Complex &z )
> {
> re = z.re;
> im = z.im;
>
> return *this;
> }
>
>
> Help!!!!


Should the "=" operation ever _change_ the value that is on the right
hand side of '='?

No?

In that case, it should be 'const'.

Should you ever be able to write e.g.


a = b = Complex( 1, 2 );

?

Yes?

In that case, the return value should be a reference so that it can
be modified (e.g., in turn invoking '=' on the result).

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Pmb
Guest
Posts: n/a
 
      05-29-2004

"Alf P. Steinbach" <> wrote in message
news:...
> * "Pmb" <> schriebt:
> > I have no idea how to write the overload function though. This doesn't

work
> >
> > Complex Complex:perator=( Complex &z )
> > {
> > re = z.re;
> > im = z.im;
> >
> > return *this;
> > }
> >
> >
> > Help!!!!

>
> Should the "=" operation ever _change_ the value that is on the right
> hand side of '='?
>
> No?
>
> In that case, it should be 'const'.


I'm not interested in worrying about const and program integrity at this
point. This code will never be used. This particular program is simply for
me to learn about writing overload functions for operators.


>
> Should you ever be able to write e.g.
>
>
> a = b = Complex( 1, 2 );
>
> ?
>
> Yes?
>
> In that case, the return value should be a reference so that it can
> be modified (e.g., in turn invoking '=' on the result).


Do you mean something like

Complex &operator=( Complex & );

Complex &Complex:perator=( Complex &z )
{
<??>
return this;
}

I still don't see what to place in the body to do the work. Suppose

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex &operator=( Complex & );
Complex conjugate();
float magnitude();
private:
float re;
float im;
};

It would seem to me that the operator function would be

Complex &Complex:perator=( Complex &z )
{
re = z.re;
im = z.im;

return this;
}

But this doesn't work.

Pmb


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-29-2004
* "Pmb" <> schriebt:
>
> "Alf P. Steinbach" <> wrote in message
> news:...
> > * "Pmb" <> schriebt:
> > > I have no idea how to write the overload function though. This doesn't

> work
> > >
> > > Complex Complex:perator=( Complex &z )
> > > {
> > > re = z.re;
> > > im = z.im;
> > >
> > > return *this;
> > > }
> > >
> > >
> > > Help!!!!

> >
> > Should the "=" operation ever _change_ the value that is on the right
> > hand side of '='?
> >
> > No?
> >
> > In that case, it should be 'const'.

>
> I'm not interested in worrying about const and program integrity at this
> point. This code will never be used. This particular program is simply for
> me to learn about writing overload functions for operators.


You will not learn about overloading in C++ if you refuse to consider
'const'.



> >
> > Should you ever be able to write e.g.
> >
> >
> > a = b = Complex( 1, 2 );
> >
> > ?
> >
> > Yes?
> >
> > In that case, the return value should be a reference so that it can
> > be modified (e.g., in turn invoking '=' on the result).

>
> Do you mean something like
>
> Complex &operator=( Complex & );
>
> Complex &Complex:perator=( Complex &z )
> {
> <??>
> return this;
> }
>
> I still don't see what to place in the body to do the work. Suppose
>
> class Complex {
> friend ostream &operator<<( ostream &, Complex & );
> public:
> Complex( float = 0.0, float = 0.0 );
> Complex operator+( Complex & );
> Complex operator-( Complex & );
> Complex operator*( Complex & );
> Complex operator/( Complex & );
> Complex &operator=( Complex & );
> Complex conjugate();
> float magnitude();
> private:
> float re;
> float im;
> };
>
> It would seem to me that the operator function would be
>
> Complex &Complex:perator=( Complex &z )
> {
> re = z.re;
> im = z.im;
>
> return this;
> }
>
> But this doesn't work.


Say rather, it does not _compile_.

First make it compile.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      05-29-2004
Pmb wrote in news: in comp.lang.c++:

> I've been working on creating a Complex class for my own learning
> purpose (learn through doing etc.). I'm once again puzzled about
> something. I can't figure out how to overload the assignment operator.
>


You're not overloading anything here, you're *declaring* and
then *defining* Complex:perator = ( Complex & );

> Here's what I'm trying to do. I've defined class Complex as
>


#include <ostream> /* always try to make your examples compile */

> class Complex {
> friend ostream &operator<<( ostream &, Complex & );


friend std:stream &operator<<( std:stream &, Complex & );

When I compiled your code the above was the only error I found.

> public:
> Complex( float = 0.0, float = 0.0 );
> Complex operator+( Complex & );
> Complex operator-( Complex & );
> Complex operator*( Complex & );
> Complex operator/( Complex & );


All of the above *create* a new value, so its appropriate that
they return by value.

> Complex operator=( Complex & );


The above dose *not* create a new value, but modifies the
'this' object, logicaly it should return Complex &.

Also it doesn't modify is argument, so you should prefer
Complex const &. As well as being "const correct" this will
allow you to later write code like:

Complex a( 1, 2 ), b( 2, 3 );

a = b.conjugate();

or:

a = a + b;

As the tempraries returned by + and .conjugate() can't be passed
to a function that takes a non-const reference.

> Complex conjugate();
> float magnitude();
> private:
> float re;
> float im;
> };
>
> The constructor is
>
> Complex::Complex( float a, float b )
> : re( a ), im( b ) { }
>
> I have no idea how to write the overload function though. This doesn't
> work
>
> Complex Complex:perator=( Complex &z )
> {
> re = z.re;
> im = z.im;
>
> return *this;
> }
>


What doesn't work about it, with the 'ostream' issue above
fixed it compiles fine for me. Are you sure the error your
seeing isn't coming from elswhere ?


Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-29-2004
> >
> > I'm not interested in worrying about const and program integrity at this
> > point. This code will never be used. This particular program is simply

for
> > me to learn about writing overload functions for operators.

>
> You will not learn about overloading in C++ if you refuse to consider
> 'const'.
>


It's worse than that. With certain compilers you will learn illegal C++. If
you then switch to a compiler that enforces const correctness you will have
to relearn C++ that you thought you already knew.

Pmb, its not so hard

Complex operator+( const Complex & ) const;
Complex& operator=( const Complex & );

john


 
Reply With Quote
 
Pmb
Guest
Posts: n/a
 
      05-29-2004

"Alf P. Steinbach" <> wrote in message
news:...
> * "Pmb" <> schriebt:
> >
> > "Alf P. Steinbach" <> wrote in message
> > news:...
> > > * "Pmb" <> schriebt:
> > > > I have no idea how to write the overload function though. This

doesn't
> > work
> > > >
> > > > Complex Complex:perator=( Complex &z )
> > > > {
> > > > re = z.re;
> > > > im = z.im;
> > > >
> > > > return *this;
> > > > }
> > > >
> > > >
> > > > Help!!!!
> > >
> > > Should the "=" operation ever _change_ the value that is on the right
> > > hand side of '='?
> > >
> > > No?
> > >
> > > In that case, it should be 'const'.

> >
> > I'm not interested in worrying about const and program integrity at this
> > point. This code will never be used. This particular program is simply

for
> > me to learn about writing overload functions for operators.

>
> You will not learn about overloading in C++ if you refuse to consider
> 'const'.


I didn't say that I refuse to consider const

pmb


 
Reply With Quote
 
Pmb
Guest
Posts: n/a
 
      05-29-2004

"John Harrison" <> wrote in message
news:...
> > >
> > > I'm not interested in worrying about const and program integrity at

this
> > > point. This code will never be used. This particular program is simply

> for
> > > me to learn about writing overload functions for operators.

> >
> > You will not learn about overloading in C++ if you refuse to consider
> > 'const'.
> >

>
> It's worse than that. With certain compilers you will learn illegal C++.

If
> you then switch to a compiler that enforces const correctness you will

have
> to relearn C++ that you thought you already knew.


I think you both read something into what I said that wasn't there. I said
*at this point*. When I learn a new language and try examples I make them as
simple as possible and work my way up. After I got this to compile and got
the logic correct, I was going to go back and place in the const.'s.

>
> Pmb, its not so hard
>
> Complex operator+( const Complex & ) const;
> Complex& operator=( const Complex & );


Still doesn't work. However I'm not sure where the error is.

thanks

Pmb


 
Reply With Quote
 
Pmb
Guest
Posts: n/a
 
      05-29-2004

"Rob Williscroft" <> wrote

> What doesn't work about it, with the 'ostream' issue above
> fixed it compiles fine for me. Are you sure the error your
> seeing isn't coming from elswhere ?


There wasn't an "ostream" issue. I simply didn't post the entire code. I
suppose I should have in retrospect. The following is the compile error I
get from the program below

-----------------
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
d:\temp\test\example.cpp:
Error E2285 d:\temp\test\example.cpp 130: Could not find a match for
'Complex:perator =(Complex)' in function main()
Error E2285 d:\temp\test\example.cpp 138: Could not find a match for
'Complex:perator =(Complex)' in function main()
Error E2285 d:\temp\test\example.cpp 139: Could not find a match for
'Complex:perator =(Complex)' in function main()
Error E2285 d:\temp\test\example.cpp 140: Could not find a match for
'Complex:perator =(Complex)' in function main()
Error E2285 d:\temp\test\example.cpp 141: Could not find a match for
'Complex:perator =(Complex)' in function main()
Error E2285 d:\temp\test\example.cpp 148: Could not find a match for
'Complex:perator =(Complex)' in function main()
*** 6 errors in Compile ***

Tool completed with exit code 1
------------------

Line 130 is " uConj = u.conjugate();"
Lines 138-141 are

138 "sum = u + v;"
139 "diff = u - v;"
140 "prod = u*v;"
141 "ratio = u/v;"


What am I doing wrong this time?

Thanks

Pmb



_____________________________________________
#include <iostream.h>
#include <math.h>

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex operator=( Complex & );
private:
float re;
float im;
};

ostream &operator<<( ostream &output, Complex &z )
{
float x = z.re, y = z.im;

if ( y == 0 ) // z = a
output << x;
else if ( x == 0 && y > 0 ) // z = i b
output << "i " << y;
else if ( x == 0 && y < 0 ) // z = - i b
output << "- i " << -y;
else if ( x > 0 && y > 0 ) // z = a + i b
output << x << " + i " << z.im;
else if ( x > 0 && y < 0 ) // z = a - i b
output << x << " - i " << -y;
else if ( x < 0 && y < 0 ) // z = -a - i b
output << x << " - i " << -y;
else if ( x < 0 && y > 0 ) // z = -a + i b
output << x << " + i " << y;

return output;
}

Complex::Complex( float a, float b )
: re( a ), im( b) { }

Complex Complex:perator+( Complex &z )
{
return Complex( re + z.re, im + z.im );
}

Complex Complex:perator-( Complex &z )
{
return Complex( re - z.re, im - z.im );
}

Complex Complex:perator*( Complex &z )
{
// z1 = x1 + i y1, z2 = x2 + i y2
//
// z1*z2 = ( x1 + i y1 )*( x2 + i y2 )
// = ( x1*x2 + i x1*y2 + i x2*y1 - y1*y2
// = ( x1*x2 - y1*y2 ) + i ( x1*y2 + x2*y1 )

float x1 = re, y1 = im, x2 = z.re , y2 = z.im;

return Complex( x1*x2 - y1*y2, x1*y2 + x2*y1 );
}

Complex Complex:perator/( Complex &z )
{
// z1 = x1 + i y1, z2 = x2 + i y2
//
// z1 z1 z2* z1*z2 z1*z2
// -- = -- -- = ------ = -------------
// z2 z2 z2* |z2|^2 x1*x1 + y1*y1
//
// z1*z2 = ( x1*x2 - y1*y2 ) + i ( x1*y2 + x2*y1 )
//
// z1 x1*x2 - y1*y2 x1*y2 + x2*y1
// -- = ----------------- + i -------------
// z2 x1*x1 + y1*y1 x1*x1 + y1*y1
//
// a = x1*x2 - y1*y2, b = x1*y2 + x2*y1, c = x1*x1 + y1*y1
//
// z1 a b
// -- = -- + i -- = x + i y
// z2 c c
//
// x = a/c, y = b/c
//
float x1 = re, y1 = im, x2 = z.re , y2 = z.im;
float a, b, c, x, y;

a = x1*x2 - y1*y2;
b = x1*y2 + x2*y1;
c = x1*x1 + y1*y1;
x = a/c;
y = b/c;

return Complex( x, y );
}

Complex Complex:perator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}


Complex Complex::conjugate()
{
return Complex( re, -im);
}

float Complex::magnitude()
{
return sqrt( re*re + im*im );
}

int main()
{
Complex u( 1, 5 ), v( 2, 2 ), w( 1, 1 );
Complex uConj, sum, prod, diff, ratio;
float r;

uConj = u.conjugate();
r = u.magnitude();

cout << "\nu = " << u << endl;
cout << "\nv = " << v << endl;
cout << "\nu* = " << uConj << endl;
cout << "\n|u| = " << r << endl;

sum = u + v;
diff = u - v;
prod = u*v;
ratio = u/v;

cout << "\nu + v = " << sum << endl;
cout << "\nu - v = " << diff << endl;
cout << "\nu*v = " << prod << endl;
cout << "\nu/v = " << ratio << endl;

sum = u + v + w;
cout << "\nu + v + w = " << sum << endl;

return 0;
}
_____________________________________________


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-29-2004

"Pmb" <> wrote in message
news:XsydnYMLl47k_SXdRVn-...
>
> "Rob Williscroft" <> wrote
>
> > What doesn't work about it, with the 'ostream' issue above
> > fixed it compiles fine for me. Are you sure the error your
> > seeing isn't coming from elswhere ?

>
> There wasn't an "ostream" issue. I simply didn't post the entire code. I
> suppose I should have in retrospect. The following is the compile error I
> get from the program below
>
> -----------------
> Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
> d:\temp\test\example.cpp:
> Error E2285 d:\temp\test\example.cpp 130: Could not find a match for
> 'Complex:perator =(Complex)' in function main()
> Error E2285 d:\temp\test\example.cpp 138: Could not find a match for
> 'Complex:perator =(Complex)' in function main()
> Error E2285 d:\temp\test\example.cpp 139: Could not find a match for
> 'Complex:perator =(Complex)' in function main()
> Error E2285 d:\temp\test\example.cpp 140: Could not find a match for
> 'Complex:perator =(Complex)' in function main()
> Error E2285 d:\temp\test\example.cpp 141: Could not find a match for
> 'Complex:perator =(Complex)' in function main()
> Error E2285 d:\temp\test\example.cpp 148: Could not find a match for
> 'Complex:perator =(Complex)' in function main()
> *** 6 errors in Compile ***
>
> Tool completed with exit code 1
> ------------------
>
> Line 130 is " uConj = u.conjugate();"
> Lines 138-141 are
>
> 138 "sum = u + v;"
> 139 "diff = u - v;"
> 140 "prod = u*v;"
> 141 "ratio = u/v;"
>
>
> What am I doing wrong this time?


Forgetting const, put the const in and it will compile.

Complex& operator=(const Complex & );

What you are failing to realise is that you cannot bind a temporary to a
non-const reference.

sum = u + v;

u + v returns a temporary, you have declared your operator= with a non-const
reference. Therefore you cannot use u + v on the right hand side of a
operator=.

Just add const.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
operator+ for complex numbers john134 C++ 7 06-29-2006 05:01 PM
<complex> : no match for 'operator*' // conversion operator double() Arvid Requate C++ 2 06-23-2006 10:41 AM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57