The following code gives me the wrong output. actually, it is not doing sum, it actually copies the 2nd object to the M3 object instead of calculating the sum. I think I have some logical errors in + operator overloading. Does anybody have any idea or any other suggestion? it displays the output actually which is called in the copy constructor function cout<data[r][c]<<"\t";. but it did not display output when I use M3.displayData(). #include
#include <string.h>
using namespace std;
class Matrix{
private:
int noOfRows;
int noOfColumns;
int **data;
public:
Matrix(int noOfRows, int noOfColumns);
void displayData();
~Matrix();
Matrix (const Matrix &ref);
Matrix operator + (Matrix m);
Matrix& operator=(Matrix m) {
std::swap(m.noOfRows, noOfRows);
std::swap(m.noOfColumns, noOfColumns);
std::swap(m.data, data);
return *this; }
};
Matrix::Matrix(int inr=0, int inc=0){
noOfRows=inr; noOfColumns=inc;
data=new int*[noOfColumns];
for(int i=0;i<noOfRows;i++)
data[i]=new int[noOfColumns];
int d;
for(int r=0;r<noOfRows;r++){
for(int c=0;c<noOfColumns;c++){
cout<<"Enter ...";cin>>d;
data[r][c]=d;
}
cout<<endl;
}
}
Matrix::Matrix (const Matrix &ref){
this->data=new int*[ref.noOfColumns];
for(int i=0;i<ref.noOfRows;i++)
this->data[i]=new int[ref.noOfRows];
for(int r=0;r<ref.noOfRows;r++){
for(int c=0;c<ref.noOfColumns;c++){
this->data[r][c]=ref.data[r][c];
cout<<this->data[r][c]<<"\t";
}
cout<<endl;
}
}
Matrix Matrix::operator + (Matrix m){
Matrix ms(m.noOfRows,m.noOfColumns);
ms=0;
for (int i=0; i<m.noOfRows; i++)
for (int j=0; j<m.noOfColumns; j++){
ms.data[i][j] = data[i][j]+m.data[i][j];
return ms;
}
}
void Matrix::displayData(){
for(int r=0;r<noOfRows;r++){
for(int c=0;c<noOfColumns;c++)
cout<<data[r][c]<<"\t";
cout<<endl;
}
}
Matrix::~Matrix(){
delete[] data;
}
int main(){
Matrix M1(2,2),M2(2,2);
cout<<"\n Matrix A="<<endl;
M1.displayData();
cout<<"\n Matrix B="<<endl;
M2.displayData();
cout<<"\n Sum of Matrix="<<endl;
Matrix M3=M1+M2;
M3.displayData();
return 0;
}
There are at least four issues with your code:
The Matrix
copy constructor fails to copy the noOfRows
and noOfColumns
values.
You erroneously allocated the row pointers by using noOfColumns
as the number of rows.
In the Matrix operator +
, you are returning the Matrix
inside the for
loop, when you should be returning it after the loop is completed.
Your destructor fails to delete[]
all the row and column data.
To fix the first two issues, since there is a lot of common code between the Matrix
default constructor and the copy constructor, you could create an Allocate
member function to allocate the memory:
class Matrix
{
private:
void Allocate();
//... other members
public:
Matrix operator + (const Matrix& m);
// other members...
};
Matrix::Matrix(int inr=0, int inc=0) : noOfRows(inr), noOfColumns(inc)
{
Allocate();
// Input code removed...
}
Matrix::Matrix (const Matrix &ref) : noOfRows(ref.noOfRows), noOfColumns(ref.noOfColumns)
{
Allocate();
for(int r=0; r < ref.noOfRows; r++)
{
for(int c=0; c < ref.noOfColumns; c++)
data[r][c] = ref.data[r][c];
}
}
void Matrix::Allocate()
{
data=new int*[noOfRows];
for(int i=0;i < noOfRows; i++)
data[i]=new int[noOfColumns]();
}
For operator +
, you should pass the Matrix
by const reference, not by value (this is in addition to fixing the mistake of returning the Matrix
prematurely):
Matrix Matrix::operator + (const Matrix& m)
{
Matrix ms(m.noOfRows,m.noOfColumns);
for (int i=0; i<m.noOfRows; i++)
{
for (int j=0; j<m.noOfColumns; j++)
ms.data[i][j] = data[i][j]+m.data[i][j];
}
return ms;
}
The last issue (the destructor) only removes the row pointers, but does not remove the data allocated for each row. The fix is below:
Matrix::~Matrix()
{
for (int i = 0; i < noOfRows; ++i)
delete[] data[i];
delete [] data;
}
Other issues:
for(int c=0;c<ref.noOfColumns;c++)
could be: for (int c=0; c < ref.noOfColumns; c++)
Excessive and unnecessary usage of this->
.
If Matrix::operator +
exists, it makes sense for Matrix::operator +=
to also exist. For the latter, operator +
would simply be implemented in terms of operator +=
, making operator +
one or two lines of code.
A probably better method of allocating the memory is illustrated by this answer , since only two allocations are done, the memory allocated is contiguous, and less memory fragmentation will occur.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.