Results 1 to 8 of 8

Thread: Newline in constant? Overloading the << operator

  1. #1

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    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!

  2. #2
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    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'

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  4. #4

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199
    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

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199
    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

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  8. #8

    Thread Starter
    Registered User struntz's Avatar
    Join Date
    Aug 1999
    Location
    Brockway,Pa,USA
    Posts
    199

    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
  •  



Click Here to Expand Forum to Full Width