c++ - Template matrix with char* consol stop responding when using operator= -
i need help. first hugh program in c++ (pointers nightmare). tried , can't find did wrong.
class aghmatrix { private: t ** matrix; int n; int m; public: aghmatrix() : n(0), m(0) {}; aghmatrix(int _n, int _m); aghmatrix(const aghmatrix& other); ~aghmatrix(); void setitem(int n, int m, t item); void setitems (t * wsk); void setitems(int r, int c ...); void setall(); void getall(); bool couldbeadded(char * word, char sign); bool equals (t first, t second); t operator()(int a, int b); aghmatrix<t> & operator+(const aghmatrix<t> & other); aghmatrix<t> & operator*(const aghmatrix<t> & other); aghmatrix<t> & operator= ( const aghmatrix<t> & other); bool operator== (const aghmatrix<t> & other); bool operator!= (const aghmatrix<t> & other); };
test :
char* cpt[] = {"to", "jest", "tablica", "wyrazow", "o", "rozmiarze", "jedenascie", "ktore", "beda", "elementami", "macierzy"}; aghmatrix<char*> cpm3(3, 1); aghmatrix<char*> cpm4; cpm3.setitems(cpt+3); cpm4 = cpm3; // here consol stop responding.
constructors :
//default aghmatrix() : n(0), m(0) {}; //with param template <class t> aghmatrix<t>::aghmatrix(int _n, int _m) { n = _n; m = _m; matrix = new t * [n]; (int = 0; < n; ++) { matrix[i] = new t[m]; } }
setitems:
template <class t> void aghmatrix<t>::setitems (t * wsk) { int licznik = 0; for(int = 0; < n; ++) { (int j = 0; j < m; j ++) { matrix[i][j] = wsk[licznik]; licznik ++; } } }
operator= char*
template <> aghmatrix<char*> & aghmatrix<char*>::operator= ( const aghmatrix<char*> & other) { if ( & other != this) { if ( (other.n == n) && (other.m == m) && (this->matrix != 0)) { (int = 0; < n; ++) { (int j = 0; j < m; j ++) { for(int ii = 0; ii < strlen(other.matrix[i][j]); ii++) { matrix[i][j][ii] = other.matrix[i][j][ii]; } } } } else { if(this->matrix) { (int = 0; < n; ++) { (int j = 0; j < m; j ++) { delete [] matrix[i][j]; } delete [] matrix[i]; } delete [] matrix; n = 0; m = 0; matrix = null; } n = other.n; m = other.m; matrix = new char **[n]; for(int = 0; < n; ++) { matrix[i] = new char*[m]; } for(int = 0; < n; ++) { for(int j = 0; j < m; j ++) { matrix[i][j] = new char[strlen(other.matrix[i][j])]; for(int ii = 0; ii < strlen(other.matrix[i][j]); ii ++) { matrix[i][j][ii] = other.matrix[i][j][ii];; } } } } } return *this; }
i tried , still i'm getting problem console (it looks i'm using somewhere uninitialized memory or sth). cant solve problem alone alone can me?
eidt: 1 more thing : error shows when program comes end , after change destructor :
now looks :
template
aghmatrix<t>::~aghmatrix() { if (matrix) { (int = 0; < n; ++) { delete [] matrix[i]; } delete [] matrix; matrix = null;; n = 0; m = 0; } }
the previous version of destructor :
template <class t> aghmatrix<t>::~aghmatrix() { if (matrix =! 0) { (int = 0; < n; ++) { delete [] matrix[i]; } delete [] matrix; matrix = 0; } }
maybe here reason of error?
edit:--------------------- found reason of error here is: when create
aghmatrix<char*> cpm3(3, 1); cpm4 = cpm3; //destructor create problem
but when this:
aghmatrix<char*>cpm3(3,2); //or somekind of other number '1' cpm4 = cpm3; // works fine
any ideas?
Comments
Post a Comment