[英]Matrix template addition operator overloading
我有一個任務是編寫一個帶有一些特定功能的模板Matrix類。 我認為operator=
, copy constructor
和parameter constructor
工作正常。 operator+
存在問題。 例如,當我添加2個僅填充數字5的矩陣時,我在整row 1
得到奇怪的數字,而整column m
在最后一行沒有元素。 這樣的事情(o - 好的,x - 錯誤的結果)。
xxxx
ooox
ooox
oooo
template <typename T> class Matrix {
int n, m; //dimensions
T** arr;
public:
Matrix();
Matrix(int row, int column, const T& value);
Matrix(const Matrix<T>& copy);
~Matrix();
void init(int row, int column, T** ar);
template <typename O>
friend std::ostream& operator<<(std::ostream& out, const Matrix<O>& t);
Matrix<T>& operator=(const Matrix<T>& rhs);
Matrix<T>& operator+=(const Matrix<T>& rhs);
Matrix<T>& operator+(const Matrix<T>& rhs);
};
template <typename T>
void Matrix<T>::init(int row, int column, T** a) {
n = row;
m = column;
arr = new int *[n];
for (unsigned i = 0; i < n; i++)
arr[i] = new int[m];
if (a) {
for (unsigned i = 0; i < n; i++)
for (unsigned j = 0; j < m; j++)
arr[i][j] = a[i][j];
}
}
template <typename T>
Matrix<T>::Matrix() {
init(2,2, arr);
}
template <typename T>
Matrix<T>::Matrix(int row, int column, const T& value) {
n = row;
m = column;
arr = new int *[n];
for (unsigned i=0; i < n; i++)
arr[i] = new int[m];
for(unsigned i=0; i<n; i++)
for(unsigned j=0; j<m; j++)
arr[i][j] = value;
}
template <typename T>
Matrix<T>::Matrix(const Matrix<T>& mat_copy) {
n = mat_copy.n;
m = mat_copy.m;
init(mat_copy.n, mat_copy.m, mat_copy.arr);
}
template <typename T>
Matrix<T>::~Matrix() {
for(unsigned i=0; i<n; i++)
delete[] arr[i];
delete[] arr;
}
template <typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& T1) {
if(this == &T1) return *this;
for(unsigned i=0; i<n; i++)
delete [] arr[i];
delete [] arr;
init(T1.n, T1.m, T1.arr);
return *this;
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const Matrix<T>& t) {
for(unsigned i=0; i<t.n; i++) {
std::cout << std::endl;
for(unsigned j=0; j<t.n; j++)
out << t.arr[i][j] << " ";
}
std::cout << std::endl;
return out;
}
template <typename T>
Matrix<T>& Matrix<T>::operator+(const Matrix<T>& rhs) {
int rows = n;
int columns = m;
Matrix result(rows,columns,0);
for (unsigned i = 0; i < rows; i++)
for (unsigned j = 0; j < columns; j++)
result.arr[i][j] = (*this).arr[i][j] + rhs.arr[i][j];
return result;
}
任何想法如何解決? 我很感激你的幫助和建議改進它,因為可能會有一些錯誤。
我使用最新的CLion版本和更新的cygwin編譯器。
我懷疑問題是由operator+()
函數的返回值引起的。 您將返回對函數本地對象的引用。
template <typename T>
Matrix<T>& Matrix<T>::operator+(const Matrix<T>& rhs) {
int rows = n;
int columns = m;
Matrix result(rows,columns,0);
for (unsigned i = 0; i < rows; i++)
for (unsigned j = 0; j < columns; j++)
result.arr[i][j] = (*this).arr[i][j] + rhs.arr[i][j];
// Returning a reference to a function local object.
// The reference will be a dangling reference when the function returns.
return result;
}
將返回類型更改為對象。
template <typename T>
Matrix<T> Matrix<T>::operator+(const Matrix<T>& rhs) {
int rows = n;
int columns = m;
Matrix result(rows,columns,0);
for (unsigned i = 0; i < rows; i++)
for (unsigned j = 0; j < columns; j++)
result.arr[i][j] = (*this).arr[i][j] + rhs.arr[i][j];
return result;
}
幾條評論:
1)(可能是你的問題的根源), operator<<
方法中的j
迭代器的上限是錯誤的,它應該是:
for(unsigned j=0; j<t.m; j++)
2)構造函數Matrix<T>(int row, int column, const T& value)
應該調用init
方法而不是重新實現它。
3) init
應該是私有的
4)提供一個創建2x2矩陣的默認構造函數是沒有意義的! 默認構造函數應該創建一個空矩陣0x0!
5)在對矩陣求和之前,應檢查它們是否與此類操作兼容。
6)我希望你不是在尋找性能,因為你在內存中放置矩陣的方式不是緩存友好而且不會很快!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.