Hi I'm writing a simple program which is basically a template for a 3D array of any data type. It is required that later on in the main function the element of the array can be accessed directly (i.e. a[1][2][3] = 3; can be used without warnings). Here is what I have so far:
template <class T>
class CArray3D {
public:
T*** tempArray;
CArray3D(int x,int y,int z){
tempArray = new T**[x];
for(int i=0;i<x;i++) {
tempArray[i] = new T*[y];
for(int j=0;j<y;j++) {
(tempArray)[i][j] = new T[z];
}
}
};
T& operator[](const int x,const int y,const int z);
};
template <class T>
T& CArray3D<T>::operator[] (const int x,const int y,const int z) {
return tempArray[x];
}
But now I get this error: overloaded operator[] must be a binary operator (has 4 parameters). I tried changing it to
operator[][][](const int x, const...)
but then I get the error: operator[] cannot be the name of a variable or data member. So I'm not sure how to do it now. Thank you!
EDIT:
The main function is given (yeah it's a homework):
int main() {
CArray3D<int> a(3,4,5);
int No = 0;
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
for(int k=0;k<5;k++)
a[i][j][k] = No++;
for(int i=0;i<3;i++)
for(int j=0;j<4;j++)
for(int k=0;k<5;k++)
cout << a[i][j][k] << ",";
return 0;
}
So I have to use a[i][j][k] here.