Inheritance question

L

Lee Garrington

Hey,

I have just revamped a program so that it makes use of inheritance and a bug
that I cannot explain has crept in.

PlayingBoard inherits from Board.

Board contains the int variables iWidth and iHeight along with a 2d array of
int defined as **board.

The PlayingBoard constructor is as follows

PlayingBoard::playingBoard(HWND hwnd, int width, int height):
Board(width, height)
{
//do some stuff
}

The Board as

Board::Board(int width, int height):
iWidth(width),
iHeight(height)
{
CreateBoard(); //creates the 2d array in memory depending on the
width/height
}

I am pretty sure the error is scope related. I can access the array within
both constructors but as soon as I come out of them I get memory violation
errors when attempting to access the board.

Where I am going wrong?

Thx in advance

Lee
 
L

lallous

Lee Garrington said:
Hey,

I have just revamped a program so that it makes use of inheritance and a bug
that I cannot explain has crept in.

PlayingBoard inherits from Board.

Board contains the int variables iWidth and iHeight along with a 2d array of
int defined as **board.

The PlayingBoard constructor is as follows

PlayingBoard::playingBoard(HWND hwnd, int width, int height):
Board(width, height)
{
//do some stuff
}

The Board as

Board::Board(int width, int height):
iWidth(width),
iHeight(height)
{
CreateBoard(); //creates the 2d array in memory depending on the
width/height
}

I am pretty sure the error is scope related. I can access the array within
both constructors but as soon as I come out of them I get memory violation
errors when attempting to access the board.

Where I am going wrong?

Thx in advance

Lee
Hello,

Can you show the code that allocates the 2d array?
 
L

Lee Garrington

board = new int *[iWidth]

for (int i = 0; i < iWidth; i++)
board = new int[iHeight]

Lee
 
L

Lee Garrington

Apologies, I am a moron.

I didnt even send in the PlayingBoard class into the View class and so it
was accessing a null pointer.

Thx anyways :)

Lee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top