c++ - Class matrix addition -


i have program suppose add 2 matrices when gets add part in main gets stuck , nothing. have messed quite time no avail. or recommendations appreciated.

#include <iostream> using namespace std;  class matrix {  private:    int **p, m, n;  public:     matrix(int row, int col)   {    m = row;    n = col;    p = new int*[m];    (int = 0; < m; i++)   p[i] = new int[n];  } ~matrix()  {    (int = 0; < m; i++)   delete p[i];    delete p;  }  void fill()  {     cout<<"enter matrix elements:";     for(int = 0; < m; i++)     {   for(int j = 0; j < n; j++)   {      cin >> p[i][j];   }    }  }  void display()  {     cout <<"the matrix is:";    for(int = 0; < m; i++)     {    cout << endl;    for(int j = 0; j < n; j++)    {       cout << p[i][j] <<" ";    }     }     cout << endl; } matrix operator +(matrix m2) {    matrix t(m, n);    for(int = 0; < m; i++)    {   for(int j = 0; j < n; j++)   {      t.p[i][j] = p[i][j] + m2.p[i][j];    }     }    return t; }  matrix operator =(matrix eq) {    m = eq.m;    n = eq.n;    p = eq.p;     return *this; }  friend matrix operator *(matrix, matrix); };  matrix operator *(matrix , matrix b) {    matrix b(1,1);    if(a.n == b.m)    {       matrix t(a.m, b.n);       for(int = 0; < a.m; i++)       {      for(int k = 0; k < b.n; k++)      {     t.p[i][k] = 0;     for(int j = 0; j < a.n; j++)     {        t.p[i][k]+= a.p[i][j] * b.p[j][k];     }  }   }   b = t;   }   return b; }  int main() {      matrix a(3,3), b(3,3);     a.fill();    a.display();     b.fill();    b.display();     cout << "addition of , b\n";    b = b + a;    b.display();     cout << "multiplication of , b\n";    b = (a * b);    b.display();   } 

your program violating rule of big three: has destructor no assignment operator , no copy constructor. keeps data using raw pointers, it's not managing proper ownership copies done , assignments performed.

when matrix class copied , assigned program entering undefined behavior territory can happen. in code copy construction done implicitly when passing matrix parameter value, , assignment done explicitly in main.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -