Overloading >> and << operators

This is a discussion on Overloading >> and << operators within the C++ Programming forums, part of the General Programming Boards category; I'm attempting to teach myself C++ and up till now its been fairly easy. I'm having tons of issues understanding ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    11

    Overloading >> and << operators

    I'm attempting to teach myself C++ and up till now its been fairly easy. I'm having tons of issues understanding overloading operators and specifically overloading >> and <<. I've been going to many sites trying to figure this out and even am using some sample code to try to give myself greater understanding. This is what I have so far in code.
    In class Rectangle:
    Code:
    friend istream& operator>>(istream& inputStream, Rectangle& Rect);
    I believe this should be done in main, but can’t see where they are doing it.
    Code:
    istream& operator>>(istream& inputStream, TRectangle& R)
    {
        cout << "Length: ";
        inputStream >> R.Length;
        cout << "Height: ";
        inputStream >> R.Height;
     
        return inputStream;
    }
    I keep getting a compile error on line
    Code:
    istream& operator>>(istream& inputStream, TRectangle& R)
    saying that I need a ';' before { but none of the syntax i've found anywhere has this ;. If I remove this bit of code, I no longer get the compile error so it has to be here.

    Any help with understanding overloading >> and << would be greatly appreciated.
    Last edited by Basca; 10-09-2006 at 01:47 PM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    In main? The operator should be overloaded outside of main. Usually, I put it above main or in a different source file with the rest of the class implementation. It's like a function. Their defined outside of main.
    Sent from my iPadŽ

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Hopefully no errors -

    Code:
    #include <iostream>
    
    class thing
    {
    private:
    	int x;
    	int y;
    
    public:
    	thing() : x( 0 ), y( 0 ) {};
    
    	friend std::istream &operator>> ( std::istream &in, thing &instance )
    	{
    		std::cout<< "Enter X value: ";
    		in >> instance.x;
    		
    		std::cout<< "Enter Y value: ";
    		in >> instance.y;
    		
    		return in;
    	}
    
    	friend std::ostream &operator<< ( std::ostream &out, thing &instance )
    	{
    		out<< "X: " << instance.x << "; Y: " << instance.y;
    		
    		return out;
    	}
    };
    
    int main( void )
    {
    	thing instance;
    
    	std::cout<< instance << '\n';
    	std::cin >> instance;
    	std::cout<< instance << '\n';
    
    	return 0;
    }
    That's just some code I made ages ago so I wouldn't forget how to do it. I think it works ...
    Last edited by twomers; 10-09-2006 at 02:12 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    11
    Well I feel like the idiot of the day. I've been working on this for over 6 hrs now, and had no other choice but to post here. Moved it out of main, and it works like a charm. Thanks for the help guys. Now just have to figure out how to output in SVG format, lol. Fun!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 04:17 AM
  2. operators << and >>
    By crvenkapa in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2007, 04:10 PM
  3. Overloading << and >>
    By Enahs in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2005, 05:33 PM
  4. << and >> overloading
    By Armatura in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2003, 06:19 PM
  5. Overloading the << and >> operators
    By WebmasterMattD in forum C++ Programming
    Replies: 9
    Last Post: 10-15-2002, 06:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21