everyone I have to make a dynamic matrix and here are the constructors and destructor I have:
Board::Board() {
a_l=0;
a_c=0;
a_mp=NULL;
}
Board::Board(const Board&t) {
a_l=t.a_l;
a_c=t.a_c;
a_mp=t.a_mp;
Memory();
copy(t);
}
Board::Board(int nl, int nc) {
a_l=nl;
a_c=nc;
Memory();
}
Board::~Board() {
freeMemory();
}
// PRIVATE METHODS
void Board::copy(const Board &t) {
int a_l, a_c;
int ** a_mp;
a_l=t.a_l;
a_c=t.a_c;
for(int i=a_l;i<a_c;i++) {
for(int j=a_c;j<a_l;j++) {
a_mp[i][j]=t.a_mp[i][j];
}
}
}
void Board::freeMemory() {
for(int i=0;i<a_l-1;i++) {
delete [] a_mp[i];
}
delete [] a_mp;
}
void Board::Memory() {
char ** a_mp;
a_mp = new char*[a_l];
for(int i =0;i<a_l; i++) {
a_mp[i]=new char[a_c];
for(int j=0;j<a_c;j++)
a_mp[i][j]='-';
}
}
Board is the class and a_l and a_c are number of lines and columns of the matrix. In my main, I declare a Board variable and then I do this:
board=Board(5,5);
It compiles, but when I want to display it, like this for example:
cout << board.Cols() << endl;
This is the method:
int Board::Cols() const {
return (a_c);
}
It displays 0. As if it didn't create board with the parameters I said.
Also the program crashes when I do this board=Board(5,5);
so I use the debugger and it says it stops at this line of the delete:
board=Board(5,5);
I don't know why it crashes and I don't know why it doesn't keep the values of the board variable I've declared! Anyone knows why?
EDIT: rMemory=Memory, it was a type from here not from the program