Access Violation - class variable.

So, about 2 hours ago I had working code.

I have a class, and I was creating shapes and storing them in a vector. But I have since changed my code and can't see why I am getting a Read - Access Violation.

This is my Shapes class:
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
41
42
43
44
45
46
47
48
49
enum facing
{
	UP,
	RIGHT,
	DOWN,
	LEFT
};

class Shapes
{
	public:
		Shapes();
		Shapes( int type, int x, int y, facing d );
		~Shapes(void);

		void createShape();

		void setShapeType( int s )	{ shapeType = s; }

		void setX( int x )		{ pos.X = x; }
		void setY( int y )		{ pos.Y = y; }
		int getX()			{ return pos.X; }
		int getY()			{ return pos.Y; }

		void setPosition( int x, int y )
		{
			pos.X = x;
			pos.Y = y;
		}

		void setFacing( facing d );

		void rotate();
		void move( facing d );

		bool canMove( facing d );
		bool canRotate( facing d );

		void setColour();
		void erase();
		void draw();

	private:
		COORD pos;
		COORD offset[ 4 ];
		facing direction;
		int shapeType;
		int colour;
};


Here's what I had before, to create/store the class. Which was working.
1
2
for( int i = 0; i < 7; ++i )
		shapeList.push_back( Shapes( i, 40, 3, UP ) );


I have since change the code to this:
Shapes *shape = new Shapes( randomRange( 0, 6 ), 40, 3, UP );

The problem I have with this, is when I check to see whether the shape can move down, or not:
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
bool Shapes::canMove( facing d )
{
	switch( d )
	{
		case LEFT:
			for( int i = 0; i < 4; ++i )
			{
				if( offset[ i ].X <= 31 )
					return false;
			}
			break;

		case DOWN:
			for( int i = 0; i < 4; ++i )
			{
				if( offset[ i ].Y >= 47 )
					return false;
			}
			break;

		case RIGHT:
			for( int i = 0; i < 4; ++i )
			{
				if( offset[ i ].X >= 50 )
					return false;
			}
			break;
	}

	return true;
}


I receive the access violation on line 16.
While debugging the program, in Visual Studio, hovering the mouse over the COORD offset, Visual Studio tells me that it's unable to read the memory.

Can someone please point me in the right direction to getting this fixed! And if you need any more information/code, just ask away!

Thanks! (:
What is COORD? How do you call canMove?
This is a COORD:
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms682119(v=vs.85).aspx

And this is how I call canMove:
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
41
42
43
44
45
void Engine::update()
{
	//update timed movements here.
	if( shape->canMove( DOWN ) )
		shape->move( DOWN );
	else
	{
		//UPDATE board here

		delete shape;

		Shapes *shape = new Shapes( randomRange( 0, 6 ), 40, 3, UP );
	}
}

void Engine::run()
{
	char key = ' ';
	int i = 0;

	gui.startScreen();

	/*gui.gameSetup();
	return;*/

	gui.drawGameGui();
	//return;

	while( true )
	{
		currentTime = GetTickCount() - startTime;

		if( currentTime > gameSpeed )
		{
			update();

			startTime = GetTickCount();
		}

		kbHit( key );

		if( ( getKeyPressed( key ) ) && ( key == 'q' ) )
			break;
	}
}


Edit:
The first shape is created in the Engine constructor:
1
2
3
4
5
6
7
Engine::Engine(void)
{
	Shapes *shape = new Shapes( randomRange( 0, 6 ), 40, 3, UP );

	gameSpeed = 200;
	startTime = GetTickCount();
}
Last edited on
You have COORD offset under a private protection level. You can't access anything under private or protected from outside the class. Try moving it to directly above the private section like this:
1
2
3
4
5
void draw();
COORD offset[ 4 ];
	private:
		COORD pos;
		
I'm accessing it from within the Shapes class.
bool Shapes::canMove( facing d )
Sory man, Dont know how on earth i missed that xD
@Lynx876
Is the shape pointer pointing to a Shapes object the first time you call the update() function?

@drew887
That's not it. canMove is a member function of the class so it can access the private members.

lol, it's alright, Drew.

And Peter, I updated my post above. I create it first in the Engine ctor.
You create a local shape pointer that point to the newly created Shapes object, so the shape pointer that you use in other places is not affected. Remove Shapes * from the start of the line is probably what you want.
Thank you!!! Very much appreciated.

I forgot to mention, I actually have Shapes *shape; as a private variable in the Engine class.

Got to love the speed of this forum! (:
Topic archived. No new replies allowed.