Instantiating a global object?

Dec 7, 2012 at 12:28am
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?
Dec 7, 2012 at 12:48am
Looks like you are dereferencing a null pointer or something. How does the Player constructor look like?
Dec 7, 2012 at 12:48am
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 Dec 7, 2012 at 12:48am
Dec 7, 2012 at 10:38am
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.
Dec 7, 2012 at 2:55pm
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?
Dec 7, 2012 at 4:46pm
struct and class are mostly the same except with the default visibility of its members, structs being public by default and classes being private.
Dec 7, 2012 at 5:26pm
Thank you Raezzor. I meant to say struct in the OP. My problem is still unanswered though. :(
Dec 7, 2012 at 10:24pm
shameless bump
Dec 7, 2012 at 10:28pm
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.
Dec 7, 2012 at 10:35pm
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.
Dec 7, 2012 at 10:43pm
Topic archived. No new replies allowed.