I'm writing a program that implements some functions of matrix but get into trouble at the beginning stage.
When debugging goes to c = c.transpose();
in the main function,it steps into the copy constructor and throws an exception at the statement:delete[]elements;
. And it says:
Unhandled exception at 0x7CBDDB1B (ucrtbased.dll) in Exercise2.2.exe: 0xC0000005: Access violation reading location 0xCCCCCCBC.
I have no idea what happens. I would appreciate it if someone could take a look at my code.
Btw, I'm a C++ beginner, so besides the error, if there are codes that are not of standard, please point it out.
header:
#include <iostream>
using namespace std;
class Matrix
{
int row;
int col;
double **elements;
public:
Matrix();
Matrix(int row, int col);
Matrix(const Matrix& srcMatrix);
Matrix& operator=(const Matrix& srcMatrix);
void setNum(double Num, int row,int col);
Matrix transpose();
void display();
};
Matrix::Matrix():elements(0),row(0),col(0){}
Matrix::Matrix(int row, int col) :row(row), col(col) {
elements = new double*[row];
for (int i = 0;i < row;i++) {
elements[i] = new double[col];
}
}
Matrix::Matrix(const Matrix& srcMatrix){
row == srcMatrix.row;
col == srcMatrix.col;
if (elements != NULL) {
delete[]elements;
}
elements = new double* [row];
for (int i = 0;i < row;i++) {
elements[i] = new double[col];
}
for (int i = 0;i < row;i++) {
for(int j = 0;j < col;j++) {
elements[i][j] = srcMatrix.elements[i][j];
}
}
}
Matrix& Matrix::operator=(const Matrix& srcMatrix) {
row == srcMatrix.row;
col == srcMatrix.col;
if (elements != NULL) {
delete[]elements;
}
elements = new double* [row];
for (int i = 0;i < row;i++) {
elements[i] = new double[col];
}
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
elements[i][j] = srcMatrix.elements[i][j];
}
}
return *this;
}
void Matrix::setNum(double Num, int row, int col) {
elements[row][col] = Num;
}
Matrix Matrix::transpose() {
Matrix temp(col,row);
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
temp.elements[j][i] = elements[i][j];
}
}
return temp;
}
void Matrix::display() {
for (int i = 0;i < row;i++) {
for (int j = 0;j < col;j++) {
cout << elements[i][j] << " ";
if (j == col - 1) {
cout << endl;
}
}
}
}
Main function:
#include<iostream>
#include "Matrix.h"
using namespace std;
int main(){
double a[3] ={ 1.0,3.0,2.0 };
double b[3] ={ 2.0,3.0,4.0 };
Matrix c(2,3);
for (int i = 0;i < 3;i++) {
c.setNum(a[i], 0, i);
c.setNum(b[i], 1, i);
}
c = c.transpose();
c.display();
return 0;
}