以下代碼沒有給出正確的輸出。矩陣資料顯示完美,但添加兩個物件 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;
}
輸出在這里了解詳細資訊
uj5u.com熱心網友回復:
復制建構式和賦值運算子都被破壞了。讓我們快速瀏覽一下,看看出了什么問題
Matrix::Matrix (const Matrix &ref){
// noOfRows and noOfColumns have not been initialized. Their values are unknown so
// the rest of this function is a crapshoot. Could do anything.
Allocate();
for(int r=0;r<ref.noOfRows;r )
for(int c=0;c<ref.noOfColumns;c )
data[r][c]=ref.data[r][c];
}
// An assignment operator is expected to return Matrix &
void Matrix :: operator = (const Matrix &M ) {
// noOfRows and noOfColumns have not been updated. Could be wrong, resulting in
// allocate allocating the wrong amount of storage. Moot point since this
// allocation is never used. See below.
Allocate();
noOfRows = M.noOfRows;
noOfColumns = M.noOfColumns;
data=M.data; // both instances share M's data. This will fail sooner or later
// If the program lives long enough, both instances will go out
// of scope and double delete the allocation.
// Also leaks allocation pointed at by `data`.
}
我很著急,所以我沒有考慮修復賦值運算子,只是使用了 Copy and Swap Idiom。很有可能有更有效的解決方案,但復制和交換通常足夠快,幾乎不可能出錯。這是一個很好的起點,基準測驗會告訴您是否存在問題。
Matrix::Matrix(const Matrix &ref):
noOfRows(ref.noOfRows),
noOfColumns(ref.noOfColumns) // initialize row and columns
{
Allocate(); // now safe to use
for (int r = 0; r < ref.noOfRows; r )
for (int c = 0; c < ref.noOfColumns; c )
data[r][c] = ref.data[r][c];
}
Matrix& Matrix::operator =(Matrix M) // correct return type, and does the heavy
// lifting with the copy constructor
{
std::swap(noOfRows, M.noOfRows);
std::swap(noOfColumns, M.noOfColumns);
std::swap(data, M.data);
return *this;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/454252.html
下一篇:如何回圈指向字符的指標陣列
