user2238244
user2238244

Reputation: 229

simply 2d array error-c++

I define an array called "matriu":

#include "CQix.h"
#include "Graphics_Console.h"


class cTauler
{

CQix Qix;
HANDLE hScreen;
int iniciX, iniciY, fiX, fiY;

private:
bool matriu[38][28];

int area_activa;

};

And I want to initialize all values to false:

void cTauler::InicialitzarTauler()
{

int i,j;


for(i=0;i<=fiX+2;i++)
{
    for(j=0;i<=fiY+2;j++)
    {
        matriu[i][j]=false;
    }
}

But when I compile i get this error: 0xC0000005: Access violation.

So i tried to define the array doing this:

bool matriu[38][28]= {false};

And I can't compile because: "not allowing data member initializer"

What can I do? Thanks.

Upvotes: 3

Views: 72

Answers (2)

John Phu Nguyen
John Phu Nguyen

Reputation: 319

Your inner loop has a faulty stop condition of

i<=fiY+2

'j' will increment through the inner for-loop, but it will not stop because 'i' is not incremented within the inner loop.

Your error is just a result of a typo. Change the inner loop to

for(j=0;j<=fiY+2;j++)

Upvotes: 1

Kupto
Kupto

Reputation: 2992

If the data matriu[38][28] is going to be always the same size, consider creating const static classmembers fiX and fiY and initialize them to values 38 and 28. You have probably not initialized them correctly...

Upvotes: 1

Related Questions