Hello..

i learn math and C++ and i thouth to combine both to make my life easeir:

i wrote a simple class that by using it i will be able to calculate relation matrix in the power n.

my problem is that in unable to store the object (it getting freed) and i can't store it.

the function bool_mutilpication need to return a object and store it inside a new one.

matrix_c = matrix_a * matrix_b

im able to do (object * object).Print();

but not object = other_object * more_other_object ; //dobule free.

Code:/*************************************************************************** * matrics.cc * * Fri Jun 1 09:23:55 2007 * Jabka Atu * Email mashrom{d0t}head@gmail.com ****************************************************************************/ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include <iostream> #include <assert.h> using namespace std; bool** multiply(const bool **matA,int rows,int colmuns,int colmuns_of_sec_matrix, const bool **matB); class bool_matrix{ int rows; int colmuns; bool** matrix; void init(); public: bool** _matrix; int size; bool_matrix(); ~bool_matrix(); bool_matrix(int m,int n); bool_matrix(const bool_matrix &other); void Print(); bool_matrix boolean_multiply(const bool_matrix &other); bool& set_values(int rows,int colmuns); bool_matrix operator * (const bool_matrix& other){ bool_matrix result(this->boolean_multiply(other)); return result; } bool_matrix& operator= (const bool_matrix &other); }; bool_matrix& bool_matrix::operator= (const bool_matrix &other){ size = other.size; rows = other.rows; colmuns = other.colmuns; matrix = other.matrix; return *this; } void bool_matrix::init(){ matrix = new bool* [rows]; assert(matrix); for (int i = 0;i < rows ; i++) { matrix[i] = new bool [colmuns]; assert(matrix[i]); } /* can be done by .. for (int i = 0;i < rows;i++) for (int j=0;j < colmuns;j++) matrix[i][j]=false; */ for (int i=0 ; i < rows * (colmuns - 1); i++) *(*matrix + i) = false; _matrix = matrix; } bool_matrix::bool_matrix() { rows = 0; colmuns = 0; size = 0; init(); } bool_matrix::~bool_matrix(){ if (!matrix) //check if there is actually allocated memory { for (int i = 0;i < rows;i++) delete [] matrix[i]; } delete [] matrix; } bool_matrix::bool_matrix(int m,int n){ rows = m; colmuns = n; size = m * n; init(); } bool_matrix::bool_matrix(const bool_matrix &other){ this -> rows = other.rows; this -> colmuns = other.colmuns; this -> size = other.size; init(); for (int i = 0;i < rows;i++) for (int j = 0;j < colmuns;j++) this->matrix[i][j] = other.matrix[i][j]; } void bool_matrix::Print(){ for (int i=0;i<rows;i++) { for(int j = 0; j< colmuns;j++) cout<<matrix[i][j]; cout<<endl; } } bool_matrix bool_matrix::boolean_multiply(const bool_matrix &other){ bool_matrix result((*this).rows ,other.colmuns); bool **returned_matrix = NULL; returned_matrix = multiply((const bool**) (*this).matrix,(*this).rows,(*this).colmuns,other.colmuns,(const bool**) other.matrix); result.matrix = returned_matrix; return result; } bool& bool_matrix::set_values(int rows,int colmuns){ return matrix[rows][colmuns]; } bool** multiply(const bool **matA,int rows,int colmuns,int colmuns_of_sec_matrix, const bool **matB){ bool **matrix; //create a new matrix for result matrix = new bool* [rows]; assert(matrix); for (int i = 0;i < rows; i++) matrix[i] = new bool [colmuns_of_sec_matrix]; assert(matrix); //End of creation of matrix. //intlize matrix for (int i = 0;i < rows ; i++) for (int j = 0 ; j < colmuns_of_sec_matrix ; j++) matrix[i][j]= false; //End initlization for (int i=0;i < rows ; i++) { for (int j=0 ;j < colmuns ; j++) for (int k=0 ; k<colmuns_of_sec_matrix ; k++) { if ((matA[i][j] == matB[j][k]) and (matA[i][j] == 1)) { matrix[i][k] = true; continue; //if even one pair (a)ij = (b)jk = 1 then (a)ik will be 1.so no need to go to check for more } } } return matrix; } int main(){ bool_matrix a(4,3),b(3,4),c; a._matrix[0][0]=1; a._matrix[0][1]=1; a._matrix[0][2]=0; a._matrix[1][0]=0; a._matrix[1][1]=1; a._matrix[1][2]=0; a._matrix[2][0]=1; a._matrix[2][1]=1; a._matrix[2][2]=0; a._matrix[3][0]=0; a._matrix[3][1]=0; a._matrix[3][2]=1; b._matrix[0][0]=1; b._matrix[0][1]=0; b._matrix[0][2]=0; b._matrix[0][3]=0; b._matrix[1][0]=0; b._matrix[1][1]=1; b._matrix[1][2]=1; b._matrix[1][3]=0; b._matrix[2][0]=1; b._matrix[2][1]=0; b._matrix[2][2]=1; b._matrix[2][3]=1; c = a * b; //dobule freea return 0; }