【发布时间】:2010-12-07 22:45:46
【问题描述】:
我正在用 C++ 实现自己的矩阵类,以帮助我加深对语言的理解。我在某处读到,如果您有一个有效的 += 运算符,请在您的 + 运算符中使用它。这就是我所拥有的:
template <class T>
const Matrix<T>& Matrix<T>::operator+(const Matrix<T> &R){
Matrix<T> copy(*this);
return copy += R;
}
这里是 += 运算符重载:
template <class T>
const Matrix<T>& Matrix<T>::operator+=(const Matrix<T> & second_matrix){
//Learn how to throw errors....
if (rows != second_matrix.getNumRows() || cols != second_matrix.getNumCols()){throw "Dimension mismatch.";}
int i,j;
for (i = 0; i < rows; i++){
for (j = 0; j < cols; j++){
data[i][j] += second_matrix.get(i,j);
}
}
return *this;
}
我可以使用 += 就好了(例如,a += b;不返回错误)。但是调用 + 运算符(例如,a = b + c;)会返回:
test.cpp.out(77055) malloc: *** error for object 0x300000004: pointer being freed was not allocated
为了完整起见,这是我的析构函数:
template <class T>
Matrix<T>::~Matrix(){
for (int i = 1; i < rows; i++){
delete[] data[i]; }
delete[] data;
}
我已经断断续续地使用 C++ 几年了,但有时仍然无法跟踪指针。我希望这是正常的... 任何帮助都会很棒。谢谢!
编辑:这是我的复制构造函数。它被设置为释放数据数组,但我删除了它。现在我遇到了分段错误。
template <class T>
Matrix<T>::Matrix(const Matrix<T>& second_matrix){
rows = second_matrix.getNumRows();
cols = second_matrix.getNumCols();
data = new T*[rows];
int i,j;
for (i = 0; i < rows; i++){
data[i] = new T[cols];
}
for (i = 0; i < rows; i++){
for (j = 0; j < cols; j++){
data[i][j] = second_matrix.get(i,j);
}
}
}
【问题讨论】:
-
你的复制构造函数是什么样的?这可能是您的问题,因为该错误表明内存被释放了两次。
-
@Walt W:是的,我怀疑这是三巨头的问题。
-
@Fred:三大问题是什么?
-
现在我们看到您有一个有效的,请参阅 Fritschy 的回答 :)
-
不要被骗养成将 const 引用视为传递值的聪明方法的坏习惯……这样做时,您应该始终考虑生命周期和可能的别名。在这种情况下,您将返回对局部变量的引用,这是不行的。