Problem with io

Hi.
I wrote a class like below:
1
2
3
4
5
6
7
8
9
10
11
class MYCLASS
{
   private:
      char Number[100];
   public:
      friend ostream& operator<<(ostream&, MYCLASS&);
      friend ofstream& operator<<(ofstream&, MYCLASS&);
      friend istream& operator>>(istream&, MYCLASS&);
      friend ifstream& operator>>(ifstream&, MYCLASS&);
      . . .
};

Why when I want to read/write from/to a file compiler calls first and third functions?
1
2
ofstream_Object << MYCLASS_Object; // this calls first function
ifstream_Object >> MYCLASS_Object; // this calls third function 
Because ifstream (and ofstream) are just specializations of istream (and ostream), (because of this whenever you call a function (or operator) that takes an ifstream as first operand the one that takes istream is seen as valid too), as indicated in this schematic table:
http://www.cplusplus.com/reference/iostream/

Inverting the order you put the function declarations might (but I guarantee nothing) change the results:
1
2
3
4
friend ofstream& ...
friend ostream& ...
friend ifstream& ...
friend istream& ...
I don't understand your question. The compiler resolves the operator to the correct function, doesn't it?
His problem is that the compiler replaces IF and OF handling to regular I and O handing.
It works as expected for me can we see more of the code?
I have two other functions except constructor and destructor till now.
1
2
MYCLASS& operator=(const MYCLASS& right);
MYCLASS& operator=(const char* rightstring);

first function also works correctly, but second doesn't.
I want to put "char* rightstring" in a file and bring it back and put it in a MYCLASS object.
Actually I want, without pressing any button on keyboard, put "rightstring" in MYCLASS object same as when we use cin object to put some input in an object.
Topic archived. No new replies allowed.