|
-
Jan 5th, 2003, 01:31 PM
#1
Thread Starter
Registered User
Newline in constant? Overloading the << operator
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
Code:
//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!
-
Jan 5th, 2003, 01:52 PM
#2
Fanatic Member
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'
-
Jan 5th, 2003, 03:09 PM
#3
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?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 03:15 PM
#4
Thread Starter
Registered User
Thanks for the replies...
Line #7 is, this line:
Code:
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
-
Jan 5th, 2003, 03:24 PM
#5
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 08:52 PM
#6
Thread Starter
Registered User
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....
Code:
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
-
Jan 6th, 2003, 12:11 PM
#7
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2003, 07:08 PM
#8
Thread Starter
Registered User
Thanks
Thanks CornedBee! You are "the man"
Everything works fine now!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|