Reputation: 465
"block.h"
enum BlockType
{
BlockType_Default = 0,
BlockType_Grass,
};
class Block
{
public:
Block();
~Block();
bool IsActive();
void SetActive(bool activeParameter);
private:
bool active;
BlockType m_blockType;
};
"block.cpp"
#include "block.h"
Block::Block()
{
m_blockType = BlockType_Grass;
active = true;
}
Block::~Block()
{
}
bool Block::IsActive()
{
return active;
}
void Block::SetActive(bool activeParameter)
{
active = activeParameter;
}
Here is my class. Now my problem is when I run the program and call the IsActive();
function, I get an error EXC_BAD_ACCESS (code=1, address = 0x0)
on the line that checks if active
is true. From what I read is what is returned if the variable doesn't exists. What is wrong with my code?
Here is where I call the function main.cpp
Block* m_pBlocks[32][32][32];
void main()
{
for(int x = 0; x < 32; x++)
{
for(int y = 0; y < 32; y++)
{
for(int z = 0; z < 32; z++)
{
printf("x:%d y:%d z:%d",x,y,z);
if(m_pBlocks[x][y][z]->IsActive())
{
//DisplayBlock
}
}
}
}
}
Upvotes: 1
Views: 101
Reputation: 20731
This statement
Block* m_pBlocks[32][32][32];
defines 32 x 32 x 32 NULL pointers. So when you try -> on those NULL pointers, it fails.
You either need to create blocks, or allocate them:
Block m_pBlocks[32][32][32];
Block* m_pBlocks[32][32][32];
m_pBlocks[x][y][z] = new Block;
Upvotes: 1