My code does not run correctly and I don't know how to fix it. This is not a duplicate question to someone asking what is the rule of three because that post does not help me in solving my question as in this post im using a pointer pointer array. I don't know what I did wrong in my big three functions but can someone please help me correct my mistake. The compiler is highlighting delete[] matrix[i]; in the the destructor when i=2 inside the for loop
In my header file I have:
#ifndef fasdf_dynn_h
#define fasdf_dynn_h
#include <iostream>
#include <fstream>
#include<string>
#include <cstdlib>
#include <vector>
using namespace std;
template <class T>
class MatrixdynVector{
public:
// MatrixdynVector()
{
//creates a 3 by 3 matrix with elements equal to 0
m=3;
n=3;
matrix=new int*[m];
for(int i=0;i<m;i++)
matrix[i]=new int[n];
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
matrix[i][j]=0;
}
// MatrixdynVector(int m,int n);
template <class H>
MatrixdynVector<H>(const MatrixdynVector<H>& c)//copy constructor
{
m=c.m;
n=c.n;
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
matrix[i][j] = c.matrix[i][j]; // add data to it
}
template <class H>
MatrixdynVector<H>& operator =(const MatrixdynVector<H>& c)//asignment
{
if (this == &c)
{
return *this;
}
else
{
matrix = new int*[c.m];
for (int i = 0; i < c.m; i++)
matrix[i] = new int[c.n]; // create a multi dimensional array
for (int i = 0; i < c.m; i++)
for (int j = 0; j < c.n; j++)
matrix[i][j] = c.matrix[i][j]; // add data to it
for (int i = 0; i < c.m; i++)
delete[] matrix[i]; // delete the second dimension of the matrix
delete[] matrix; // delete the first*/
return *this;
}
}
~MatrixdynVector()
{
if(matrix!=NULL)
{
for (int i = 0; i < m; i++)
delete[] matrix[i]; // delete the second dimension of the matrix
delete[] matrix; // delete the first
matrix=NULL;
}
}
private:
int m,n;
int** matrix;
};
#endif