tags:

views:

58

answers:

2

I'm having this weird problem: when my program reach this method:

//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;
}

it suddenly crashes...

When i ran it with my VS debugger, it looked just all fine, the new matrix was filled with the relevant values, till the line return result; that from some mysterious reason returned an empty matrix vector.

Where do i go wrong??


Here is my implementation for the 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; 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];
        }
    }
}

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;
}
A: 

You are returning the matrix by value. A copy constructor gets involved. How is your copy constructor defined?

Alex Emelianov
//CCtor of RegMatrix RegMatrix::RegMatrix(const RegMatrix 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]; } }}
limlim
Put that into your question, properly formatted please.
PigBen
(i've added it to my question so it would look nicer)
limlim
A: 

Assuming that MyDouble has a correct copy constructor, you should be able to reduce your copy constructor to just this:

RegMatrix::RegMatrix(const RegMatrix &other):numRow(other.getRow()), numCol(other.getCol()),
    _matrix(other._matrix)
{ }

See what that gets you.

Edit: Your assignment operator might be a problem if the columns and rows aren't equal. You're throwing an assert in that instance, so the program is going to abort. Is that what you want? Wouldn't you rather the assignment change the columns and rows to match the new values? If so, you can just do this:

RegMatrix & RegMatrix::operator=(const RegMatrix & rhs) {
    if(this == &rhs)
        return *this;
    numRow = rhs.getRow();
    numCol = rhs.getCol();
    _matrix = rhs._matrix;
    return *this;
}
PigBen
it causes me some even more mysterious problems..what's wrong with my current CCtor?
limlim
Probably nothing, it just contains unnecessary code, so it's more prone to have errors. I just figure if you make your constructor simpler, you can be sure that it's not the problem. See Alex's comment, we now need to see your assignment operator. If you don't have one, then we'll need to see your class body.
PigBen
What sort of 'more mysterious problem' does this cc cause?
Timo Geusch
here you go, i added also operator= implementation. what i don't get is that the debugger turn "result" variable to be an empty vector before it goes out of the method scope, and then crashes..
limlim
What's the crash look like? If you place a breakpoint on the `assert` line of the assignment operator, does it get there before the crash?
Alex Emelianov
thank you very much! The assert line really was the problem!
limlim