Instantiating a global object?

I have a class in Player.h called Player. I have included it in the Main.cpp and outside of the main function i have written the line

Player player;.

When the program tries to execute that line i get an unhandled exception: access violation writing location 0x0000000004.

Are you just not allowed to instantiate global objects or something?
Looks like you are dereferencing a null pointer or something. How does the Player constructor look like?
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Player
{
Player()
	{
		xPos = 100;
		yPos = 100;
		xVel = yVel = 0;

		texture.loadFromFile("Load/ballTexture");
		sprite.setTexture(texture);
	}
...
}
Last edited on
Is texture and sprite members of Player? Be aware that the Player constructor could be called before main() so if any of the code used here depend on some initialization done in main you have a problem.
Hello nano551,
sorry to jump in, but isnt that a struct you are creating? how do you instantiate that? i thought you meant it was a class?
struct and class are mostly the same except with the default visibility of its members, structs being public by default and classes being private.
Thank you Raezzor. I meant to say struct in the OP. My problem is still unanswered though. :(
shameless bump
It's not possible to see what the problem is from the code you have posted. It could be something wrong with the loadFromFile and setTexture functions, or with the texture and sprite constructors, or it could be something else. Using a debugger to find the problem is probably the easiest way so I recommend you learn how to use a debugger.
I should have mentioned im using sfml. I did notice though that i forget to add ".png" to the end of the filename. That, however, was not the problem.

While stepping through the constructor with the debugger, i get the break message after stepping over the line Player(). I think this means that there is nothing wrong with the code in the constructor. Please help.
Topic archived. No new replies allowed.