Overloading Output Operator Question

So I'm trying to figure out how to overload the output operator " << ". I've looked it up on google, and all I can find is this solution:

In header file:
 
friend ostream &operator<<( ostream &output, const object &obj );


and in cpp file:
1
2
3
4
5
ostream &operator<<(ostream &output, const object &obj )
{ 
    output << obj.variable1 << " " << obj.variable2;
    return output;            
}


But when I try to implement this in my code, I get loads of errors to do with the use of "friend" and "ostream".

Any help would be much appreciated.

I'm coding on Visual Studio, and can post my own code if needed.
This example:
http://msdn.microsoft.com/en-us/library/vstudio/1z2f6c2k.aspx

worked fine for me.

Can you get that to run?
Nope that's the one that I tried to use and was turning up errors.

Also do functions in the cpp file not usually have to have the class name in them?

So what I mean is if I have a function void add(); in a class maths, in the cpp file isn't is declared as void maths::add(){ .... }?
Got it, didn't have <iostream> in my header file.
Topic archived. No new replies allowed.