Friend func. and overloaded >> and <<
I checked the FAQ on this, and someone had posted this very same question before, but the solution doesn't work for me. I have a class declaration (CFract) in a header file, and under the public section I have this:
Code:
friend istream& operator>>(istream&, CFract&);
friend ostream& operator<<(ostream&, CFract&);
In a source file, I have this (not within the main function, that's in a different source file):
Code:
ostream& operator<<(ostream& os, CFract& rF)
{
if (rF.denom==1)
os << rF.num;
else
os << rF.num << "/" << rF.denom;
return os;
}
istream& operator>>(istream& is, CFract& rF)
{
char c;
is >> rF.num >> c >> rF.denom;
return is;
}
Visual C++ says istream and ostream cannot access the private variables of the class CFract (num and denom). There's no reason why the istream and ostream functions shouldn't be able to access them. The solution in the FAQ I found was to put the #include<iostream> and using namespace std; statements at the top (duh), and the person says that fixed the problem...but I always had those statements, and it still doesn't work. I remembered to include the header file (yes, using quotation marks). Any ideas? I'll post the full code if necessary.