0

I want to pass the result object to another display function,but for some reason its not working. cmd stops working

I tried using different aproaches but none seems to be working.. Basically I ahbe to add two matrices using a function and return type should be of an object. I want to print the result of this addition, not in this function but using another function.

#include<iostream>
using namespace::std;

class Matrix{
private:
    int row,column; //dimensions row x column
    int **matrix; //pointer to a pointer to int
    void allocarray(){ //method to llocate array matrix and the matrix[i] arrays
        matrix=new int*[row];
        for(int i=0;i<row;i++){
            matrix[i]=new int[column];
        }
    }

public:
    Matrix(int rowsize, int columnsize); //default constructor
    Matrix(); //user defined constructor
    ~Matrix(); //destructor
    void input();
    Matrix Add(Matrix);
    void display(Matrix);
};
Matrix Matrix::Add(Matrix m2)
{
    Matrix result(3,3);
    for(int i=0;i<row;i++)
    {
        for( int j=0;j<column;j++)
        {
            result.matrix[i][j]=this->matrix[i][j]+m2.matrix[i][j];
        }
    }

    return *this;

}
void Matrix::display(Matrix m)
{
    for(int i=0;i<row;i++)
    {
        for( int j=0;j<column;j++)
        {
            cout<<m.matrix[i][j];
        }
        cout<<endl;
    }
}


Matrix::Matrix( int rowsize, int columnsize):row(rowsize),column(columnsize) //dynamivally allocate 
{
    allocarray();
    for(int i=0;i<row;i++)
    {
        for( int j=0;j<column;j++)
        {
            matrix[i][j]=0; //initilze all values to 0
        }
    }
}
Matrix::~Matrix() //destructor
{
    for( int i=0;i<row;i++)
    {
        delete [] matrix[i];
    }
    delete [] matrix;
}

void Matrix::input()
{
    cout<<"enter the elements for the matrix"<<endl;
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<column;j++)
            cin>>matrix[i][j];
            cout<<"\n"; //check it after performing functions!
    }
}
int main()
{
     Matrix obj1(3,3),obj2(3,3),res(3,3);
     cout<<"enter elements for matrix one";
     obj1.input();
     cout<<"enter elements for matrix two";
     obj2.input();
     cout<<"addition of two matrices";
     res=obj1.Add(obj2);
     obj1.display(res);

     return 0;

}

so here's the code for copy constructor

Matrix::Matrix(const Matrix &m):row(m.row),column(m.column)
{
allocarray();
for(int i=0;i<row<i++)
{
for(int j=0;j<column;j++)
{
matrix[i][j]=m.matrix[i][j];
}
}
}
  • 4
    Your matrix manages resources, so you need to follow [the rule of three](http://en.wikipedia.org/wiki/Rule_of_three_%28C++_programming%29), or use some storage class that does the management for you. – juanchopanza Nov 17 '13 at 13:53

3 Answers3

1

Your class doesn't define a copy constructor or an assignment operator. But any attempt to pass a matrix to a function or return a matrix from a function copies the matrix. Your code fails because the destructor is freeing memory that is still in use. When writing classes that manually manage memory you must follow the rule of three. Do this and your program will work.

Also as Patato said you need to add return result; to Matrix::Add, but that will also fail unless you follow the rule of three.

EDIT: changed the link to point to the Stack Overflow page on the rule of three, which is much more helpful than the Wikipedia page.

EDIT: here's an example copy constructor for this class, it's fairly similar to the regular constructor

Matrix::Matrix(const Matrix& rhs):row(rhs.row),column(rhs.column) 
{
    allocarray();
    for(int i=0;i<row;i++)
    {
        for( int j=0;j<column;j++)
        {
            matrix[i][j]=rhs.matrix[i][j];
        }
    }
}
Community
  • 1
  • 1
john
  • 85,011
  • 4
  • 57
  • 81
  • CAN u help me with the code..? i know the concept of copy constructor, i have written the code for it but dnt know to implement it in this code...i am stuck with it frm the past 2 hours – user3001571 Nov 17 '13 at 14:38
  • @user3001571 See update, in this case the copy constructor is very similar to the constructor you have already written. Don't forget you need to do the assignment operator too. If you follow the copy and swap idiom (explained here http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) that's very easy too. – john Nov 17 '13 at 14:42
  • i did exactly the same thing..should i have to change anything in the add function..? – user3001571 Nov 17 '13 at 14:42
  • @user3001571 You add function is confused, you put the answer into `result` but then you `return *this;`, one or the other not both. It's like the difference between `+=` and `+`, you have to decide which you want. – john Nov 17 '13 at 14:44
0

You could either pass a reference to the function (and return one too):

Matrix& Add(Matrix&);

or write a copy constructor, so that you can use the actual matrix later.

Also, you shouldn't be returning the matrix you're adding to, but the result:

Matrix& Matrix::Add(Matrix &m2)
   {
    Matrix *result = new Matrix(3,3);
    for(int i=0;i<row;i++)
    {
        for( int j=0;j<column;j++)
        {
            result->matrix[i][j]=this->matrix[i][j]+m2.matrix[i][j];
        }
    }
    return *result;  
   }
Eutherpy
  • 4,471
  • 7
  • 40
  • 64
  • Your version leaks memory. Defining a copy constructor isn't optional for this kind of class. – john Nov 17 '13 at 14:14
0

using reference is good, and your add function does not return the add result so it looks like this

   Matrix Matrix::Add(Matrix &m2)
   {
    Matrix result(3,3);
    for(int i=0;i<row;i++)
    {
        for( int j=0;j<column;j++)
        {
            result.matrix[i][j]=this->matrix[i][j]+m2.matrix[i][j];
        }
    }
    return result;  
   }

but it is still not very good, after this function the result will be deleted

Patato
  • 1,464
  • 10
  • 12
  • i am looking to return the result to main, then passing it to display function which has been called with obj1,and finaaly printing it in proper matrix form – user3001571 Nov 17 '13 at 14:13