我有这个奇怪的问题:当我的计划达成这个方法:有问题的矢量返回值 - 没有真正更新?
//Returns the transpose matrix of this one
RegMatrix RegMatrix::transpose() const{
RegMatrix result(numCol,numRow);
int i,j;
for(i=0;i<numRow;++i)
for(j=0;j<numCol;++j){
result._matrix[j][i] = _matrix[i][j];
}
return result;
}
突然崩溃......
,当我和我的VS调试运行它,它看起来只是所有优良,新的矩阵填充了相关的值,直到return result;
从某个神秘的原因返回空矩阵向量。
我在哪里出错?
这里是我的拷贝构造函数的实现:
//CCtor of RegMatrix
RegMatrix::RegMatrix(const RegMatrix &other): numRow(other.getRow()), numCol(other.getCol()){
//Create
_matrix = vector<vector<MyDouble> >(other.getRow());
int i,j;
for(i=0; i < numRow; i++)
_matrix[i] = vector<MyDouble>(other.getCol());
//Copy Matrix
for(i=0;i<numRow; ++i){
for(j=0;j<numCol; ++j){
_matrix[i][j] = other._matrix[i][j];
}
}
}
我的赋值操作符的实现:
//RegMatrix = RegMatrix
RegMatrix& RegMatrix::operator=(const RegMatrix rhs){
assert(numRow == rhs.getRow() && numCol == rhs.getCol());
if(*this != rhs){
int i,j;
for(i=0;i<numRow;++i)
for(j=0;j<numCol;++j){
_matrix[i][j] = rhs._matrix[i][j];
}
}
return *this;
}
你的副本构造函数是什么样的? – 2010-10-07 22:48:32
你的拷贝构造函数做了很多和不必要的拷贝(见PigBen的答案),但最终它应该工作。我能想象的代码是这样'RegMatrix换位= someMatrix.transpose();'所以接下来的犯罪嫌疑人是你的赋值运算符 – 2010-10-07 23:09:22