I am trying to declare a 2 streams, 1 for input and one for output, I know how to do this normally, but I am trying to do it now in a class, and over a header and cpp file which I have never done and I can not seem to figure it out, and I am getting errors with it.
The errors seem to be in the header file and is complaining about a type specifier and something about a dummy = int not being a type name. But this is how I normally declare the streams when not normally done in a class, should this be done differently now that it is in a class?
I was thinking I should maybe declare the header as myOutStream("string" ) but I am not sure what the ios::app should be, I read that a flag is a bool but when i tried myOutStream("string" , bool) it still does not work. Could someone please point me in the right direction.
header
1 2 3 4 5 6 7 8
class book
{
private:
ofstream myOutStream("Books.txt", ios::app);
ifstream myInStream("Books.txt");
}
So far I think it will always be append. (Havent though too far ahead)
When i tried it this way.
1 2
ofstream myOutStream;
ifstream myInStream;
I get errors because the arguments are different between the header file and the cpp file for the two functions. Also getting an E error code about the two functions saying "call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type"
Use braces, not parentheses, e.g.: ofstream myOutStream{"Books.txt", ios::app};
If I remember correctly, parentheses can't be allowed because it would produce ambiguities in C++'s formal grammar (i.e., they're banned for no important reason).
to clarify:
using the {} locks you to the one file name. Is the name always the same?
myOutStream("Books.txt", ios::app); //I thought you wanted a METHOD because of this line in the cpp file and your comments, so that is what I gave you.
I see now you wanted a VARIABLE, which is what Mbozzi gave you.