Matrix Class Error

Hello all, this is my first time of using cpluscplus, nice to meet you all.
I am writing a matrix class that overloads assignments,addition, subtraction...etc.
The assignment operator "=" overloading part seems to work fine, but if I attempt to overload the addition "+" operator, I get error message like this:

"Debug Assertion Failed!
Program:...nts\C++\week6....\week6matrix.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgdel.cpp
Line:52

Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)"

But if I remove "delete[]matrix" in my destructor, everything seems to work, but due to the requirement of this project, I needed to have this term in my destructor.

I would have posted a picture but its my first time of using this website, so I dont know how to do that, I apologise if my question doesnt seem to make sense, but I ll try my best to explain it if you have questions regarding this issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include <stdlib.h>
using namespace std;
//dynamic matrix
class dymatrix
{
    friend ostream & operator << (ostream &os, dymatrix &om);
    friend istream & operator >> (istream &is, dymatrix &om);
private:
    int rows;
    int columns;
    double *matrix;
public:
	
    dymatrix(){cout<<"Default constructor called"<<endl; columns = 0; rows=0; matrix=0;}
    dymatrix(int inrows, int incolumns){
		rows = inrows;
		columns = incolumns;
		matrix = new double [inrows*incolumns];
		for (int i=0; i<inrows*incolumns; i++) { 
			matrix[i]=0;
		}
	}
    int lengthr() const {return rows;}  //Returns number of rows.
    int lengthc() const {return columns;}   //Return number of columns.
	dymatrix& operator=(dymatrix&);
	~dymatrix(){cout<<"Destructor called"<<endl;delete[] matrix;}  

    int index(int i, int j)  //This member function returns the position of each index.
    {
        if (j > 0 && j <=rows && i > 0 && i <=columns)
        {
            return (i-1)+(j-1)*columns;
        }
        else {cout<<"Error, out of range"<<endl; exit (1);}
    }
		double & operator()(int i, int j) {return matrix[index(i,j)];}  //The operator () returns the position of j and i in 1D array.
	
	dymatrix operator + (dymatrix &arr)  //overloading addition.
    {   
		
        if (rows !=arr.rows && columns != arr.columns)
        {
            cerr<<"SIZE DO NOT MATCH, YOU FAIL"<<endl; exit(1);
        }
        dymatrix new_matrix(rows,columns);
        for (int j = 0; j < arr.rows*arr.columns; j++)
        {
            //for (int i = 1; i <= arr.columns; i++)
            //{
            //cout<<"****"<<j<<endl;    
			new_matrix.matrix[j]= matrix[j]+arr.matrix[j]; //Putting in the data into this dynamic array for each element.
            //}
        }
		return new_matrix;
    }
	
	};  //Class end.
	dymatrix & dymatrix::operator = (dymatrix &arr) //Overloading "=" operator.
    {
		if (&arr == this) return *this; //If the array is the same, no need to change, just to print. The key word "this" is a pointer to the object, and *this gives the object.
		delete[] matrix; matrix =0; rows =0; columns =0;
		rows = arr.rows;   //Setting row length.
		columns = arr.columns;    //Setting column length.
		if(rows*columns > 0)
        {
            matrix = new double [rows*columns]; //Defining a dynamic array here.
            for (int j = 1; j <= rows; j++) //Assigning each term to each term.
            {
                for (int i =1; i <=columns;i++ )
                {
                matrix[index(i,j)] = arr(i,j);  //This is the assigning part, the loop above loops everything so that each term is assigned.
                }
            }
        }
        return *this;   //Return
    }

    istream & operator >> (istream &is, dymatrix &om)   //Overloading ">>" operator here to
    {
        cout<<"Please enter the number of rows you want"<<endl;
        is >> om.rows;  //Inputting number of rows.
        cout<<"Enter the number of columns you want"<<endl;
        is >> om.columns;   //Inputting number of columns.
        cout<<"Enter matrix"<<endl;
        om.matrix = new double [om.rows*om.columns];    //Making a dynamic array here to put the data in.
        for (int j = 1; j <= om.rows; j++)
        {
            for (int i = 1; i <= om.columns; i++)
            {
                is >> om.matrix[om.index(i,j)]; //Putting in the data into this dynamic array for each element.
            }
        }
        return is;
    }
    ostream & operator << (ostream &os,   dymatrix &om)  //To output the matrix in an standard matrix way
    {
        for(int j= 1; j<=om.rows; j++)
        {
            os<<endl<<endl;
            for (int i = 1; i <=om.columns;i++)
            {
                os << om.matrix[om.index(i,j)]<<"\t";   //Similar method used in istream.
            }
        }
        return os;
    }
int main()
{
    dymatrix a1;
    cin >> a1;  //Define the rows of the matrix
    cout << a1<<endl<<endl;
    dymatrix a2;
    cin >> a2;
	cout << a2<<endl<<endl;
   dymatrix resu_a1;
	resu_a1=a1+a2;
	cout<<"Addition = "<<resu_a1<<endl;
	dymatrix resu_a3;
	resu_a3 = a1;
	cout<<"Assigning = "<<resu_a3<<endl;
    return 0;
}
You have failed to implement a copy constructor. The default copy constructor generated by the compiler does not do what you want.
Hello LB

Thanks I realised that yesterday and now it is solved, thanks
Topic archived. No new replies allowed.