Rob Williscroft said:
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;
}
_____________________________________________