I tried implementing a template Array-class that allows initial size of global arrays to be known at compile time, specified by the user.
When I specify a class of type <1, 1> size, the file compiles fine. However, when I specify a class of any other numeric type, in either "parameter" where both aren't 1, I get this message--
-------------- Build: Debug in Reform_2DArray ---------------
main.cpp
Process terminated with status -1073741819 (0 minutes, 1 seconds)
0 errors, 0 warnings
via Code::Blocks IDE, Microsoft Visual C++ 2005/2008 compiler, on Windows XP.
The code is a modified version of something I posted in someone elses topic. It doesn't quite solve their problem, but it does provide an interesting means of cleanly creating a 2D array at compile time.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::size_t;
template< size_t, size_t> class TwoDimensional;
template< size_t R, size_t C>
class TwoDimensional{
private:
enum {TRUE_R = 2 * TwoDimensional<R-1, 0>::TRUE_R};
enum {TRUE_C = 2 * TwoDimensional<0, C-1>::TRUE_C};
// int dummyArray[TRUE_R][TRUE_C];
// int row[TRUE_R][TRUE_C];
//public:
/*
TwoDimensional(){
int myArray[TRUE_C] = {0};// initialize to have 0 in all indices
// cout << "Initializing 2Darray to be of 'area' : [" << TRUE_R << " x " << TRUE_C << "]" << endl;
for(size_t i = 0; i < TRUE_R; i++)
memcpy(row[i], myArray, TRUE_C * sizeof(int)); // copy contents of myArray into row[i]
}
size_t getRowSize(){
return TRUE_R;
}
size_t getColSize(){
return TRUE_C;
}
ostream& showContents(ostream& out = cout){
for(size_t i = 0; i < TRUE_R; i++){
for(size_t j = 0; j < TRUE_C; j++)
out << row[i][j] << " ";
out << endl;
}
return out;
}
int* operator[] (size_t indice){
if(indice >= 0 && indice < TRUE_R){
return row[indice];
}
return dummyArray[indice];
}*/
};
template<>
class TwoDimensional<0, 0>{
public:
enum{TRUE_R = 1};
enum{TRUE_C = 1};
};
int main(){
TwoDimensional<1, 1> td; // error if any other number, other than 1, is enterred for either parameter
// td.showContents() << endl;
cin.ignore();
cin.get();
return 0;
}
I've tried changing the access modifier to public, but no luck.
I'm still trying to figure this problem out, but I'm fairly stumped! O_O
Thanks =)
-Alex