Initial Problem Fixed, New Question regarding program crashing.

EDIT: Please look down at the newer posts to see my current question. My program is crashing when I start it. The code posted here is what I'm having trouble with.


Because I have no idea what's going on here. It was to my understanding that anything you include in your main .cpp file would be carried over to header files. My compiler decided that my Map.h file isn't using namespace std anymore. When I go to compile, it spits out a ton of errors about things being undefined, all fixed if I put std:: in front of them. Putting a cout in Game.cpp works just fine.

I've never encountered this before... Could somebody explain to me what is going on?

Map.h:
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
class world{
private:
	world *playerLocation;
	world *map[25];
	world *north;
	world *south;
	world *east;
	world *west;
	string location;
	string description;

public:
	void createMap();
	void displayCells();
	world();
};

world::world(){

}

void world::createMap(){

	string loc;
	ifstream readWorld;
	readWorld.open("World.txt");

	for(int i = 0; i < 25; i++){
		getline(readWorld, loc);
		map[i]->location = loc;	
	}
	readWorld.close();
}

void world::displayCells(){

	for(int i = 0; i < 25; i++){
		cout<<map[i]->location<<endl;
	}
}


Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>
#include <fstream>
#include <iostream>
#include "Map.h"
using namespace std;

int main(){

	world worldMap;
	worldMap.createMap();
	worldMap.displayCells();
	cin.ignore();
	cin.get();
	return 0;
}


Also, without the namespace error, I also get a crash when I run the program. If somebody could help me with that as well, your assistance would be appreciated.

Thanks!

EDIT: Changing the first block of code because my copy/paste skills failed me...
Last edited on
It was to my understanding that anything you include in your main .cpp file would be carried over to header files

Nope, it's the other way around in certain cases but generally header files don't get anything from the main block of your code. Also I hope there is more to your "Map.h", because you're not actually declaring a class here...
Actually, your compiler is behaving as it should. To be precise, your header file is inserted into your main.cpp file before the compilation. The problem is, the using namespace std comes AFTER that. You #include and use all namespaces your header needs in the header aswell, don't worry about including things twice, headers are protected with something like this:
1
2
3
4
5
#ifndef HEADERNAME_H_INCLUDED
#define HEADERNAME_H_INCLUDED

//code
#endif 


That way the preprocessor will prevent including the same file twice. Writing using namespace std multiple times won't hurt either.

Also, your header file doesn't contain a class declaration, but strangely enough class function definitions, which do not belong in a header file at all! Maybe you should review the concept of header files once more before you attempt anything else.
Last edited on
Thanks. As you can probably tell I'm new to header files but I appreciate your advice.
You would have to include the libs your using and it looks like the std namespace as well. I was just pointing out that you didn't post the entrypoint for your class with my earlier comment, and it won't work with out that.
What do you mean by entrypoint for the class?
This is how it works: all an #include does is copy text from a file into the current one before compilation. What's compiled is the result of that. So, for the above program to work as you intended, you could swap lines 4 and 5 and it would work. But don't. Having a header depend on something like that is a really bad idea. And adding using namespace std to a header isn't a good idea either.

Just to illustrate, technically, you can even do this:

1
2
3
// header.h
a = 2;
cout << a << endl;

1
2
3
4
5
6
7
8
9
10
// main.cpp
#include <iostream>

using namespace std;

int main()
{
    int a;
    #include "header.h"
}
Last edited on
Same thing that hanst99 said, judging by the rest of your code you should have a line at the top of Map.h that says something along the lines of:
1
2
class world {
/* The rest of your code */

You probably just didn't hightlight that part on your copy paste though.

EDIT: Just to make sure it's clear what filipe is pointing out in his code is one of those "just because you can, doesn't mean you should" moments.
Last edited on
Yea, I just noticed that. Copied wrong.
Well to prevent starting another topic, can somebody help me figure out what's going on here? When I try to run the program, I get this error:

Unhandled exception at 0x0006609b in Data Structures Game.exe: 0xC0000005: Access violation reading location 0xcccccd5c.

It points to this line of code in xstring:
1
2
if (this->_Myres < _Newsize)
		_Copy(_Newsize, this->_Mysize);	// reallocate to grow 


Any ideas as to why that's not working?
Is that an OoB? Are you using Dev-C++? I used to see stuff like that when ever I started a new project with XP SP3 and that IDE.
Last edited on
I'm using Visual C++ 2010 Express.
Would this be an issue with allocating memory? If so, how would I fix that while still maintaining the functionality of the array?


EDIT: It looks like this is a problem with the array not actually being created. It would be easy if not for the fact that it needs to be type world.

I could do
map = new world[25];
but in createMap(), I get an error saying that map must have pointer type. Does anybody have any idea how to create this array? Everything I've found on the internet is for standard types (int, char, etc.)

EDIT: I got rid of the pointer type error by changing the "->" to a "." The program still crashes though.

Last Edit: Fixed it... forgot to call the function that creates the array.... Problem solved.
Last edited on
Topic archived. No new replies allowed.