PDA

Click to See Complete Forum and Search --> : Newline in constant? Overloading the << operator


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

riis
Jan 5th, 2003, 12:52 PM
What are those two single quotes doing in that line? If you replace them with double quotes (and maybe a space or another character inside), then you might be able to compile your program. Single quotes are normally used for representing character values, like char x = 'a'

CornedBee
Jan 5th, 2003, 02:09 PM
If he wants to output a single character single quotes are ok. But he should have something in between, e.g. ' '

Which line is #7?

struntz
Jan 5th, 2003, 02:15 PM
Thanks for the replies...

Line #7 is, this line:

ostream& operator<<(ostream &ostrm, const Complex &x)
{
return ostrm << x.top << '\' << x.bottom;

}


and there is infact a \ in those ' ' but u can't see it becuase this messageboard must not like the \, so it doesn't appear in the post

CornedBee
Jan 5th, 2003, 02:24 PM
Is there a double \\ in the ''? Else the compiler will assume you want to escape the closing ' and is therefore completly right to complain about too many characters and a newline in the constant.

struntz
Jan 5th, 2003, 07:52 PM
Thanks Bee! it works!
but how can I make it output the backslash character?> '\' didn't work neither did "\"
also, The << works, but now the >> doesn't work....

istream& operator>>(istream &istrm, const Complex&x)
{
cout << "Enter numerator: "; istrm >> x.top;
cout << "Enter denominator: "; istrm >> x.bottom;
return istrm;
}


when I do the following....
int main(void)
{
Complex x;
cin >> x;
return 0;
}

the console window gets flooded when I run the program

CornedBee
Jan 6th, 2003, 11:11 AM
First answer: '\\'

Second answer: const Complex&x
This means the object you pass is not modifiable. But you want to modify it, so the reference must not be const.
Complex&x

struntz
Jan 6th, 2003, 06:08 PM
Thanks CornedBee! You are "the man"
Everything works fine now!
:D