Declaring streams in header and cpp file

Hello !

I want to mention 2 stream for input and output , normally I have an idea about its use but now I am going to perform it in class, now I am facing errors with cpp file and header which I have not tried before and it seems impossible to solve it. The error appears to be in header and making complain about dummy = int regarding type name and about type specifier. But usually announce streams in this way when not done in class, so there should be a different way for doing in class? I am in doubt about header may be it should be declared myOutStream("string" ) but I am not clear about  ios::app that what it should be. I studied about bool that it’s a flag but in my case when I used, it failed to work.
Please someone give me suggestions. https://www.theengineeringprojects.com/2021/10/list-of-ides-to-run-c-programs.html

cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void book::writeToFile(book const& obj)
{
	myOutStream("Books.txt", ios::app);
	if (!myOutStream)
	{
		cout << "Error opening file\n";
	}


}

void book::readFromFile()
{
	myInStream("Books.txt");


}

header
1
2
3
4
5
6
class book
{
private:
	ofstream myOutStream("Books.txt", ios::app);
	ifstream myInStream("Books.txt");
}

Thanks.
Last edited on
> now I am facing errors
post the errors verbatim

¿how many times do you want to construct your stream?
You don't use L3/L14. L4 opens the file for output. There is a problem with L5 as that opens the same file for reading. If you want to read/write from/to the same file, open it once as fstream (with the appropriate flags - in/out/app etc).

Often you would open the file/test if the file was opened in the constructor as appropriate.
Topic archived. No new replies allowed.