Friend operator << with ostream
Hi All,
I'm creating a Fraction class, for study purposes and I'm struggling with a friend operator function in my class.
this, defined in my header file, within my class.
Code:
private:
int num;
int den;
public:
friend ostream &operator<<(ostream &os, Fraction &fr);
Then, if i put the following in my .cpp file - it cant cant access the internal methods or data of my class.
Code:
ostream &operator<<(ostream &os, Fraction &fr)
{
os << fr.num << "/" << fr.den;
return os;
}
I thought the whole point of the friend keyword was to define the method as a global function that still has access to the methods and data as a local method has?
In addition to this, if i put the declaration of my method in the header file, but outside of the class, i get several error msg's when compiling, listed below.
Code:
syntax error : missing ';' before '&'
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(22): error C2433: 'ostream' : 'friend' not permitted on data declarations
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(22): error C2061: syntax error : identifier 'ostream'
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(22): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(22): error C2805: binary 'operator <<' has too few parameters
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C2143: syntax error : missing ';' before '&'
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C2086: 'int ostream' : redefinition
1> c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(22) : see declaration of 'ostream'
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C2065: 'os' : undeclared identifier
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C2065: 'fr' : undeclared identifier
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C2275: 'Fraction' : illegal use of this type as an expression
1> c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(7) : see declaration of 'Fraction'
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\puppy\documents\visual studio 2010\projects\finalfraction\finalfraction.h(34): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Does anyone have any idea on this. I'd really like to use this method.
Thanks
Freem