I have this array class listed below. I also have a 2d version. The 2d version works well, but when I use the 3d version I get exceptions being thrown. The exception is thrown on a cout call and not anything to do with my 3d array! Could someone help me figure this out.
3D Array code:
#ifndef ARRAYS_H
#define ARRAYS_H
#ifdef WIN32
#pragma once
#endif
template < class T > class Array3d
{
private:
T *m_pArray;
int m_Width;
int m_Height;
int m_Depth;
public:
Array3d(int Width, int Height, int Depth)
{
this->m_pArray = new T[Width * Height * Depth];
if (this->m_pArray == NULL)
{
this->m_Width = 0;
this->m_Height = 0;
this->m_Depth = 0;
}
else
{
this->m_Width = Width;
this->m_Height = Height;
this->m_Depth = Depth;
}
}
~Array3d()
{
if (this->m_pArray != NULL)
{
delete [] this->m_pArray;
this->m_Width = 0;
this->m_Height = 0;
this->m_Depth = 0;
this->m_pArray = NULL;
}
}
bool Resize(int Width, int Height, int Depth)
{
T *newArray = new T[Width * Height * Depth];
if (newArray == NULL)
return false;
int minWidth = (Width < this->m_Width) ? Width : this->m_Width;
int minHeight = (Height < this->m_Height) ? Height : this->m_Height;
int minDepth = (Depth < this->m_Depth) ? Depth : this->m_Depth;
this->m_Width = Width;
this->m_Height = Height;
this->m_Depth = Depth;
int offset1 = 0;
int offset2 = 0;
int offset3 = 0;
int offset4 = 0;
for (int z = 0; z < minDepth; z++)
{
offset1 = z * minWidth * minHeight;
offset3 = z * this->m_Width * this->m_Height;
for (int y = 0; y < minHeight; y++)
{
offset2 = y * minWidth;
offset4 = y * this->m_Width;
for (int x = 0; x < minWidth; x++)
newArray[offset1 + offset2 + x] = this->m_pArray[offset3 + offset4 + x];
}
}
if (this->m_pArray != NULL)
delete [] this->m_pArray;
this->m_pArray = newArray;
newArray = NULL;
return true;
}
T& Index(int x, int y, int z)
{
return this->m_pArray[(z * this->m_Width * this->m_Height) + (y * this->m_Width) + x];
}
int Width()
{
return this->m_Width;
}
int Height()
{
return this->m_Height;
}
int Depth()
{
return this->m_Height;
}
int Size()
{
return this->m_Width * this->m_Height * this->m_Depth;
}
};
#endif
Test application:
#include "stdafx.h"
#include <Arrays.h>
#include <iostream>
using namespace std;
void Array3dTest();
void Array2dTest();
void ArrayTest();
int _tmain(int argc, _TCHAR* argv[])
{
Array3dTest();
return 0;
}
void Array3dTest()
{
Array3d<int> test(5, 5, 2);
for (int z = 0; z < test.Depth(); z++)
{
for (int y = 0; y < test.Height(); y++)
{
for (int x = 0; x < test.Width(); x++)
test.Index(x, y, z) = x;
}
}
cout << "Values:" << endl;
for (int z = 0; z < test.Depth(); z++)
{
>>ERROR HERE cout << z << ": " << endl;
for (int y = 0; y < test.Height(); y++)
{
cout << y << ": ";
for (int x = 0; x < test.Width(); x++)
cout << test.Index(x, y, z) << " ";
cout << endl;
}
}
cout << endl << endl;
test.Resize(10, 10, 2);
for (int z = 0; z < test.Depth(); z++)
{
for (int y = 0; y < test.Height(); y++)
{
for (int x = 0; x < test.Width(); x++)
test.Index(x, y, z) = x;
}
}
cout << "Values:" << endl;
for (int z = 0; z < test.Depth(); z++)
{
cout << z << ": " << endl;
for (int y = 0; y < test.Height(); y++)
{
cout << y << ": ";
for (int x = 0; x < test.Width(); x++)
cout << test.Index(x, y, z) << " ";
cout << endl;
}
}
}
The error happens on the line indicated above. Any clues?
[Edited by - Akusei on January 26, 2006 8:20:07 PM]