Can't use streams properly in a member function of a class

I am trying to read a map from text file to an array by using a member function of a class but I am getting lots of errors which don't indicate where the problem is...Though, I believe it's in the member function loadEasyMap(char Map[][8]) because my code ran before using it.

This is my header file
1
2
3
4
5
6
7
8
9
10
#include<fstream>
using namespace std;
class DrawMap
{
public:
	DrawMap();
	~DrawMap();
	void loadEasyMap(char Map[][8]);
};

This is my cpp file
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
#include "DrawMap.h"
#include <fstream>
using namespace std;


DrawMap::DrawMap()
{
	
}


DrawMap::~DrawMap()
{
}
void DrawMap::loadEasyMap(char Map[][8])
{
	char b;
	ifstream source;
	source.open("Easy.txt");
	if (!source) {
		cerr << "error!!\n";
		exit(1);
	}

	for (int r = 0; r < 8; r++)
		for (int c = 0; c < 8; c++)
		{
			source >> b;
			Map[r][c] = b;
		}

	}
	source.close();
 }

That's my main file
1
2
3
4
5
6
7
int main()
{
char Map[8][8];
DrawMap OurMap;
OurMap.loadEasyMap(Map);
}
return 0;



Last edited on
Don't include <fstream> in the header file (unless you are actually using it in the header file itself). And NEVER EVER say using namespace std in a header. Let the user of the header file decide if they want to dump std into the global namespace or not.

You need to include <iostream> as well as <fstream> in the source file.

You are missing an opening brace at the end of the first for statement.

return 0 is outside of main!
tpb (380)
Don't include <fstream> in the header file (unless you are actually using it in the header file itself). And NEVER EVER say using namespace std in a header. Let the user of the header file decide if they want to dump std into the global namespace or not.

You need to include <iostream> as well as <fstream> in the source file.

You are missing an opening brace at the end of the first for statement.

return 0 is outside of main!

Ouch sometimes it takes someone else to look at your code so that you can find silly mistakes..Thank you :)
Topic archived. No new replies allowed.