c++ - Problematic vector return value - not really updated? -
i'm having weird problem: when program reach method:
//returns transpose matrix of 1 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; }
it crashes...
when ran vs debugger, looked fine, new matrix filled relevant values, till line return result;
mysterious reason returned empty matrix vector.
where go wrong??
here implementation copy constructor:
//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; < 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]; } } }
my assignment operator implementation:
//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; }
assuming mydouble has correct copy constructor, should able reduce copy constructor this:
regmatrix::regmatrix(const regmatrix &other):numrow(other.getrow()), numcol(other.getcol()), _matrix(other._matrix) { }
see gets you.
edit: assignment operator might problem if columns , rows aren't equal. you're throwing assert in instance, program going abort. want? wouldn't rather assignment change columns , rows match new values? if so, can this:
regmatrix & regmatrix::operator=(const regmatrix & rhs) { if(this == &rhs) return *this; numrow = rhs.getrow(); numcol = rhs.getcol(); _matrix = rhs._matrix; return *this; }
Comments
Post a Comment