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?
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...
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:
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.
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.
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 usingnamespace 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>
usingnamespace std;
int main()
{
int a;
#include "header.h"
}
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.