OK so I have read through all the answers and understand how to fix the issue now, so thank you all. However, I have some follow up questions, just to make sure I am fully understanding how eveything works
Firstly, regarding the header guards rather than pragma. I am learning from pluralsight and the woman doing the tutorial said she is not going to teach the header guards, and they are too confusing, and to just use pragma, should I not pay attention to this and instead look into the header guards then?
Secondly, I have looked more into const functions, and have learned that const functions can not call member functions that are not const themselves, (can not find anything on friend functions). I understand that the problem with the code was the stream trying to be changed. But is the reason it was not working originally because the stream was trying to be changed in the writeToFile function, or is it because the writeToFile function was calling a non const function(which, as i said above not sure if it can do, since it is a friend function) and that was trying to change the stream.
Thirdly, This code is just for practice so I am just thinking and testing out different ways of doing things. I understand that the best way to deal with the stream is probably the way keskiverto showed. But just thinking, would another suitable way be to declare the stream as a static variable, so that only 1 version of it would exist between all of the versions of the object?
Lastly, I just want to make sure I am understanding this correctly
The reason why the A (Book::myOutStream) is const is that the myOutStream is a member of Book and the A << B; statement is within const member function of Book (which treats all members of *this as const). |
So, myOutStream is const because the function writeToFile is const. And since obj is a parameter which is a book object, and this book object was passed in as an argument it has a *this pointer, which refers to all the member variables, and in this case these are all const. And these are all const because the function the book obj is passed into is a const function?