Thread: ostream
View Single Post
Old Nov 10th, 2005, 1:38 PM   #10
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
#include <iostream>

using namespace std;

class foo
{
	int a;
public:
	friend ostream& operator << ( ostream & os, foo const & f  )
	{
		os << f.a << std::endl;
		return os;
	}
};

int main()
{
	foo f;

	cout << f << endl;

	return 0;
}

That should work ^^ (it does for me).

What I said before about needed the friend because the operator wasnt a member function was wrong, its just the way I learnt it. There are some problems with the stream's not working with class operators only with global ones (because of the ordering of variables I think), maybe some one else with help clarify the stituation.
Animatronic is offline   Reply With Quote