C++ 中矩阵的加法使用面向对象编程
以下代码未给出正确的输出。矩阵数据显示完美,但添加两个对象M1和M2后,没有显示正确的输出。如果我使用 setData 在矩阵中输入数据,数据会完美存储,但加法无法正确执行。请向我建议如何纠正这个逻辑错误?
#include <iostream>
#include <string.h>
using namespace std;
class Matrix{
private:
void Allocate();
int noOfRows;
int noOfColumns;
int **data;
public:
Matrix(int noOfRows, int noOfColumns);
void setData();
void displayData();
~Matrix();
Matrix (const Matrix &ref);
Matrix operator + (const Matrix &m);
void operator = (const Matrix &M );
Matrix& operator = (int x);
};
Matrix::Matrix(int inr=0, int inc=0){
noOfRows=inr; noOfColumns=inc;
Allocate();
}
Matrix::Matrix (const Matrix &ref){
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 :: operator = (const Matrix &M ) {
Allocate();
noOfRows = M.noOfRows;
noOfColumns = M.noOfColumns;
data=M.data;
}
Matrix& Matrix :: operator = (int x){
Allocate();
for(int r=0;r<noOfRows;r++)
for(int c=0;c<noOfColumns;c++)
data[r][c]=x;
return *this;
}
void Matrix::Allocate(){
data=new int*[noOfRows];
for(int i=0;i<noOfRows;i++)
data[i]=new int[noOfColumns]();
}
void Matrix::setData(){
for(int r=0;r<noOfRows;r++){
for(int c=0;c<noOfColumns;c++){
cout<<"Enter ...";cin>>data[r][c];
}
cout<<endl;
}
}
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;
}
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(){
for (int i = 0; i < noOfRows; ++i)
delete[] data[i];
delete [] data;
}
int main(){
Matrix M3(2,2);M3=0;
Matrix M1(2,2);M1=1;
Matrix M2(2,2);M2=2;
//M1.setData();M2.setData();M3.setData();
cout<<"\n Matrix A = "<<endl;
M1.displayData();
cout<<"\n Matrix B = "<<endl;
M2.displayData();
cout<<"\n Matrix C = "<<endl;
M3 = M1;
M3.displayData();
cout<<"\n Sum of Matrix = "<<endl;
M3 = M1 + M2;
M3.displayData();
return 0;
}
the following code is not giving the correct output. Matrix data is displaying perfectly, but after the addition of two objects M1 and M2, it did not display the correct output. If I use setData to input data in the matrix, data is stored perfectly, but the addition is not performing correctly. kindly suggest to me how can I correct this logical error?
#include <iostream>
#include <string.h>
using namespace std;
class Matrix{
private:
void Allocate();
int noOfRows;
int noOfColumns;
int **data;
public:
Matrix(int noOfRows, int noOfColumns);
void setData();
void displayData();
~Matrix();
Matrix (const Matrix &ref);
Matrix operator + (const Matrix &m);
void operator = (const Matrix &M );
Matrix& operator = (int x);
};
Matrix::Matrix(int inr=0, int inc=0){
noOfRows=inr; noOfColumns=inc;
Allocate();
}
Matrix::Matrix (const Matrix &ref){
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 :: operator = (const Matrix &M ) {
Allocate();
noOfRows = M.noOfRows;
noOfColumns = M.noOfColumns;
data=M.data;
}
Matrix& Matrix :: operator = (int x){
Allocate();
for(int r=0;r<noOfRows;r++)
for(int c=0;c<noOfColumns;c++)
data[r][c]=x;
return *this;
}
void Matrix::Allocate(){
data=new int*[noOfRows];
for(int i=0;i<noOfRows;i++)
data[i]=new int[noOfColumns]();
}
void Matrix::setData(){
for(int r=0;r<noOfRows;r++){
for(int c=0;c<noOfColumns;c++){
cout<<"Enter ...";cin>>data[r][c];
}
cout<<endl;
}
}
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;
}
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(){
for (int i = 0; i < noOfRows; ++i)
delete[] data[i];
delete [] data;
}
int main(){
Matrix M3(2,2);M3=0;
Matrix M1(2,2);M1=1;
Matrix M2(2,2);M2=2;
//M1.setData();M2.setData();M3.setData();
cout<<"\n Matrix A = "<<endl;
M1.displayData();
cout<<"\n Matrix B = "<<endl;
M2.displayData();
cout<<"\n Matrix C = "<<endl;
M3 = M1;
M3.displayData();
cout<<"\n Sum of Matrix = "<<endl;
M3 = M1 + M2;
M3.displayData();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
复制构造函数和赋值运算符都被破坏了。让我们快速浏览一下,看看出了什么问题,
我很着急,所以没有花太多心思去修复赋值运算符,只是使用了 复制和交换惯用法。很可能存在更有效的解决方案,但复制和交换通常足够快并且几乎不可能出错。建立一个很好的起点,基准测试会告诉您这是否是一个问题。
Copy constructor and assignment operator were both broken. Let's take a quick stroll to see what went wrong
I was in a hurry so I didn't put much thought into fixing the assignment operator and just used the Copy and Swap Idiom. It's very possible that there is a more efficient solution, but copy and swap is usually fast enough and almost impossible to get wrong. Makes a great starting point and benchmarking will tell you if it's a problem.