试图为矩阵重载*运算符

时间:2014-11-03 01:46:38

标签: c++ overloading

所以我试图重载*运算符,使它乘以2个矩阵,这些矩阵存储在int向量的向量中。但是,当我输入原始矩阵时,我的代码似乎总是给我错误的输出:

当我做{0,1,0},{0,2,0},{0,3,0} * {0,2,0},{0,2,0},{0,2 ,0}程序给了我:

0
0
0
0
4
4
0
0
0
0
4
4
0       4       0
0       4       2
0       0       0

而不是

0 2 0
0 4 0
0 6 0

运算符*

Matrix Matrix::operator*(Matrix m){
    vector<int> mRow;
    vector<int> mCol;
    vector<int> newRow;
    Matrix newM(row, column);

    for(int i =0; i<row-1; j++){
        for(int j = 0; j<row-1; i++){
            mRow = getRow(j);
            mCol = m.getCol(j);
            int r = 0;
            int c = 0;
            int product = 0;
            do{
                product+=mRow[r]*mCol[c];
                ++r;
                ++c;
                cout << product << endl;
            }while(r < row && c < column);          
            newRow.push_back(product);
        }
        newM.setRow(newRow,i);
        newRow.clear(); 
    }
    return newM;
}

1 个答案:

答案 0 :(得分:2)

for(int j = 0; j<row-1; i++){
//      ^      ^        ^

糟糕。