Thread: overloaded << operator doesnt work

  1. #1
    Unregistered
    Guest

    overloaded << operator doesnt work

    I have a problem printing to a file, this is the main code:
    Code:
    //debuggin' code
    	for( i = 0; i < polys.size( ); i++ )
    	{
    		fileOut << "POLYGON " << i << " :" << endl;
    		fileOut << "Normal : " << *polys[ i ].getNormal( ) << endl;		//no operator << defined??
    //		cout << "Normal : " << *polys[ i ].getNormal( ) << endl;			//prints to screen as expected
    
    		for( int j = 0; j < polys[ i ].getCornerCount( ); j++ )
    		{
    			Vector3d temp( polys[ i ].getPoint( j ) );
    //			fileOut << temp << endl;
    		}
    		fileOut << endl << endl;
    	}
    and this is the overloaded << operator:
    Code:
    ostream& operator << ( ostream& os, const Vector3d&a )
    {
    	os << "( " << a.triple[ 0 ] << " , " << a.triple[ 1 ] << " , " << a.triple[ 2 ] << " )";
    	return os;
    }
    The getNormal(...) function returns a pointer which can be dereferenced and printed to the screen but not to file. Why is this?? The other commented out line "fileOut << temp << endl;" should also work in the same way but doesn't. Is it the overloaded operator or something else?

  2. #2
    Unregistered
    Guest
    oh yeah, and the fileOut variable has been declared like this, in case its useful:

    Code:
    	ofstream fileOut( "test.txt", ios::out );

  3. #3
    Unregistered
    Guest
    A quick lookup of ostream on MSDN tells me that ostream is not used for files...instead that is an ofstream. You use the ostream in your overloaded function...which is associated with output to the screen. This is why you can print to the screen but not to a file.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Whoops...wasn't signed in. O yeah...good tip is always to goto MSDN...it has the answers to most things if you know where to look. If you didn't know, a good shortcut is to highlight a word and hit F1...it will take you to the appropriate article in MSDN.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    I looked a little more, and it seems that fstream and ostream are deff not inherited from a common base class...so it looks like you have to overload the function again to take an ofstream object.
    ______________________
    The Gekko

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ostream is the base for ofstream -

    template <class Elem, class Tr = char_traits<Elem> >
    class basic_ofstream : public basic_ostream<Elem, Tr>

    so that isn't the problem. From the code posted it's hard to say where the error is.

  7. #7
    Unregistered
    Guest
    What else can I post to give you a better idea?

    ps I changed it to ofstream and now I am getting ambiguous symbol errors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. Friend func. and overloaded >> and <<
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2005, 01:14 AM
  5. Why won't my OpenGL work?
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 11-26-2005, 11:53 AM