struntz
Jan 5th, 2003, 12:31 PM
hello everyone!
I'm getting back into programming once again! wee.
Okay, anyways, I was trying to overload the ostream operator << so i could output my Complex numbers by using, Complex x(3,4); cout <<"x=" << x << endl;
so here is my Complex.h
//complex.h
class Complex
{
friend ostream& operator<<(ostream &ostrm, const Complex& x);
friend istream& operator>>(istream &istrm, const Complex &x);
int top;
int bottom;
//constructors
public:
Complex(int x = 0, int y = 1)
{ top = x; bottom = y;}
//copy constructor
Complex(Complex &x)
{
top = x.top;
bottom = x.bottom;
}
Complex operator +(const Complex &b)
{
Complex c;
c.top = top + b.top;
c.bottom = bottom + b.bottom;
return c;
}
Complex operator -(const Complex &b)
{
Complex c;
c.top = top - b.top;
c.bottom = bottom - b.bottom;
return c;
}
Complex operator *(const Complex &b)
{
return (Complex(top * b.top, bottom * b.bottom));
}
void operator+=(Complex &b)
{
top = top + b.top;
bottom = bottom + b.bottom;
}
void operator+()
{
cout << top << "\\" << bottom << endl;
}
};
//friend functions
ostream& operator<<(ostream &ostrm, const Complex &x)
{
return ostrm << x.top << '\' << x.bottom; //error here
}
istream& operator>>(istream &istrm, const Complex&x)
{
cout << "Enter numerator: "; istrm >> x.top;
cout << "Enter denominator: "; istrm >> x.bottom;
return istrm;
}
here are my errors:
--------------------Configuration: ComplexII - Win32 Debug--------------------
Compiling...
complex.cpp
C:\unzipped\VC++\ComplexII\complex.cpp(7) : error C2001: newline in constant
C:\unzipped\VC++\ComplexII\complex.cpp(7) : error C2015: too many characters in constant
C:\unzipped\VC++\ComplexII\complex.cpp(9) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.
ComplexII.exe - 3 error(s), 0 warning(s)
Any idea of what i'm doing wrong?
Thanks! :D
I'm getting back into programming once again! wee.
Okay, anyways, I was trying to overload the ostream operator << so i could output my Complex numbers by using, Complex x(3,4); cout <<"x=" << x << endl;
so here is my Complex.h
//complex.h
class Complex
{
friend ostream& operator<<(ostream &ostrm, const Complex& x);
friend istream& operator>>(istream &istrm, const Complex &x);
int top;
int bottom;
//constructors
public:
Complex(int x = 0, int y = 1)
{ top = x; bottom = y;}
//copy constructor
Complex(Complex &x)
{
top = x.top;
bottom = x.bottom;
}
Complex operator +(const Complex &b)
{
Complex c;
c.top = top + b.top;
c.bottom = bottom + b.bottom;
return c;
}
Complex operator -(const Complex &b)
{
Complex c;
c.top = top - b.top;
c.bottom = bottom - b.bottom;
return c;
}
Complex operator *(const Complex &b)
{
return (Complex(top * b.top, bottom * b.bottom));
}
void operator+=(Complex &b)
{
top = top + b.top;
bottom = bottom + b.bottom;
}
void operator+()
{
cout << top << "\\" << bottom << endl;
}
};
//friend functions
ostream& operator<<(ostream &ostrm, const Complex &x)
{
return ostrm << x.top << '\' << x.bottom; //error here
}
istream& operator>>(istream &istrm, const Complex&x)
{
cout << "Enter numerator: "; istrm >> x.top;
cout << "Enter denominator: "; istrm >> x.bottom;
return istrm;
}
here are my errors:
--------------------Configuration: ComplexII - Win32 Debug--------------------
Compiling...
complex.cpp
C:\unzipped\VC++\ComplexII\complex.cpp(7) : error C2001: newline in constant
C:\unzipped\VC++\ComplexII\complex.cpp(7) : error C2015: too many characters in constant
C:\unzipped\VC++\ComplexII\complex.cpp(9) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.
ComplexII.exe - 3 error(s), 0 warning(s)
Any idea of what i'm doing wrong?
Thanks! :D