fstream doesn't work inside header file

it works when called from the main() function but it doesn't work when its called from the class inside the header file

its saying that fstream functions can't even be recognised when they are inside my class
Post your header file, and the exact error messages you are getting.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
class CFileStorage
{//this class writes onto the file Name&Wins.txt 
public:
	void Read()
	{
		
		//the initilisation to open the file is this section
		//this enables the file reading to take place
		ifstream qGameSave("Name&Wins.txt");
		
		std::string qLine;
		//this statements tests whether the file has been open or note
		//if the file has been openned,

		if (qGameSave.good())
		{
			//the statement in side the while loop below means that the loop will
			//only continue if the end of file(eof) hasn't been reached
			while(!qGameSave.eof())
			{
				//this function reads a line from the file and then stores it onto the srting object qLine
				getline(qGameSave, qLine);
				//this section prints the lines aquired by the std::string qLine
				std::cout << qLine <<std::endl;
			}
		}
		else
		{		std::cout<<"error unable to open GameSave file"<<std::endl;  }

		//close the file, this important since the file could remain in the memory

		qGameSave.close();
		//the file is then closed after this
		
}




	void SaveGame(std::string xPlayer, int iWins)
	{
		using namespace std;
		ofstream qGameSave("Name&Wins.txt");
		//the while loop will only work whist the file is openned, 
		//this helps to prevent errors that may arise a code section tries to execute an unavaible objects
		if (qGameSave.good())
		{
			//below are the messages to be included after the previous game
			std::string qLine1("PREVIOUS GAME SAVED:");
			std::string qLine2("PLAYER NAME     ");
			std::string qLine3("PLAYER WINS OUT OF 10 ARE,      ");
			
			//this section is used for writing the messages on to the text file saving the player's gaming results
			qGameSave << qLine1<<endl;
			qGameSave << qLine2 << xPlayer << endl;
			qGameSave << qLine3 << iWins << endl;


			if (iWins >= 5)
			{
				//the win case is true, this message will be written
			std::string qLine4("WON THIS GAME");
			qGameSave << qLine4<< endl;
			}
			else
			{
				std::string qLine5("LOST THE GAME");
				qGameSave << qLine5<< endl;
			}
		
		}

		//close the file, this important since the file could remain in the memory

	}
};
If this is your entire header file then you are missing stuff.

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef BLESSMAN11_CFILESTORAGE_HPP
#define BLESSMAN11_CFILESTORAGE_HPP

#include <fstream>
#include <string>

class CFileStorage
{
/* stuff from above goes here */
};

#endif 

Don't forget to prefix STL classes with std::. That is, everywhere you have ifstream you should have std::ifstream.

I don't know why, but you have already done that with std::string...

Good luck!
Topic archived. No new replies allowed.