PDA

View Full Version : Array question



td
17th August 2009, 12:28
Hi,

I have a qwidget, that has as a member a 2-D array of ints. Now I'm having trouble accessing the vales in member functions of the widget.


// the .h file
class holdingWidget : public QWidget
{
Q_OBJECT
.
.
.
.
protected:
void paintEvent(QPaintEvent *ev);

private:
int myArrayOne[44][3];

Now in the class defintion

holdingWidget::holdingWidget(QWidget* parent): QWidget(parent)
{
int myArrayOne[44][3] =....
.
.
.
}

void holdingWidget::paintEvent(QPaintEvent *ev)
{
.
.
int k = myArrayOne[0][0];
int l = myArrayOne[0][1];
}


Now in the paintEvent the values are nonsense numbers and not the ones I specified above. But if I define the array in the paintevent it works ok. What am I missing here? Any help appreciated.

PaceyIV
17th August 2009, 12:37
You define the int myArrayOne[44][3]; in the class header, so in the constructor you don't have to re-define myArrayOne.
myArrayOne defined in the constructor is a private variable visible only in the constructor. It is different from the one defined in the class as private member.

try



holdingWidget::holdingWidget(QWidget* parent): QWidget(parent)
{
for (int r=0; r < 44; ++r)
for (int c=0; c<3; ++c)
myArrayOne[r][c] = 1;


}


EDIT: There was a trivial error

td
17th August 2009, 13:15
Ah I knew it would be something stupid like that that I'd miss! Thanks a lot. Works now.

td
17th August 2009, 16:38
Ok so when initialising the array


for(int i=0;i<44;i++)
{
for(int j=0;j<3;j++)
{
qDebug() << "For " << i << " - " << j;
myArrayOne[i][j] = 1;
qDebug() << myArrayOne[i][j];
}
}

The program won't build and crashes. Get the following error


Unhandled exception at 0x6703f24c (QtCored4.dll) in myProgram.exe: 0xC0000005: Access violation reading location 0x00000035.

Any ideas?

PaceyIV
17th August 2009, 18:05
Use the debugger.
I don't think that the error you reported it's here!

Boron
17th August 2009, 18:12
And give your class an upper case first letter.
class HoldingWidget It's a world wide accepted convention to do this :).