2 Replies - 11331 Views - Last Post: 30 March 2010 - 12:32 PM Rate Topic: -----

#1 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Overloaded Operator for Matrix Multiplication

Posted 30 March 2010 - 11:14 AM

I have been working on this problem for about a week and can not seem to get the overloaded operator to work. I am overloaded the multiplication operator to multiply two matrices together. I have tried numerous different techniques and I am left at this current state. I would appreciate it greatly if someone could look over my overloaded operator and give me a clue as to what I am doing wrong. The two errors I am getting at the moment are...

Quote

||=== hw7-q1, Release ===|
C:\Users\***\Desktop\School\c++\HW7\hw7-q1\matrix.cpp||In function `int** operator*(const Matrix&, const Matrix&)':|
C:\Users\***\Desktop\School\c++\HW7\hw7-q1\matrix.cpp|107|error: no match for 'operator[]' in 'm1[i]'|
C:\Users\***\Desktop\School\c++\HW7\hw7-q1\matrix.cpp|107|error: no match for 'operator[]' in 'm2[k]'|
||=== Build finished: 2 errors, 0 warnings ===|


Here is the code for the header file...

#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED

const int ROWS = 3, COLS = 3;

class Matrix{
    public:
        Matrix();
        Matrix(int ROWS, int COLS);

        void input();
        void output();

        void set();

        friend int** operator *(const Matrix& m1, const Matrix& m2);

    private:
        int rows, columns;
        int** m;
        int** n;
        int** a;

};

#endif // MATRIX_H_INCLUDED



and the implementation file...

#include <iostream>
#include "matrix.h"

using namespace std;

Matrix::Matrix()
{
    m = new int*[ROWS];
    for( int i = 0; i < ROWS; i++ )
        m[i] = new int[COLS];
    rows = ROWS;
    columns = COLS;

    n = new int*[ROWS];
    for( int i = 0; i < ROWS; i++ )
        n[i] = new int[COLS];

    a = new int*[ROWS];
    for( int i = 0; i < ROWS; i++ )
        a[i] = new int[COLS];
}

Matrix::Matrix(int r, int c)
{
    rows = r;
    columns = c;
    m = new int*[rows];
    for( int i = 0; i < rows; i++ )
    m[i] = new int[columns];

    n = new int*[rows];
    for( int i = 0; i < rows; i++ )
    n[i] = new int[columns];

    a = new int*[rows];
    for( int i = 0; i < rows; i++ )
    a[i] = new int[columns];

}

void Matrix::output()
{
    cout << "First Array:\n";
    for( int i = 0; i < rows; i++)
    {
        for( int j = 0; j < columns; j++)
            cout << m[i][j] << " ";
        cout << endl;
    }
    cout << "Second Array:\n";
    for( int i = 0; i < rows; i++)
    {
        for( int j = 0; j < columns; j++)
            cout << n[i][j] << " ";
        cout << endl;
    }
}

void Matrix::input()
{
    cout << "BEGIN INPUT\n";
    cout << "First Array:\n";
    for( int i = 0; i < rows; i++)
    {
        //cout << i<< endl;
        //cout << rows << endl;
        for( int j = 0; j < columns; j++)
        {
            //cout << j << endl;
            //cout << columns << endl;
            cout << "Input value for m[" << i+1 << "][" << j+1 << "] : ";
            cin >> m[i][j];
        }
    }
    cout << "Second Array:\n";
    for( int i = 0; i < rows; i++)
    {
        //cout << i<< endl;
        //cout << rows << endl;
        for( int j = 0; j < columns; j++)
        {
            //cout << j << endl;
            //cout << columns << endl;
            cout << "Input value for n[" << i+1 << "][" << j+1 << "] : ";
            cin >> n[i][j];
        }
    }
    cout << "END INPUT\n";


}

int** operator *(const Matrix& m1, const Matrix& m2)
{
    int** a;

    a = new int*[ROWS];
    for( int i = 0; i < ROWS; i++ )
        a[i] = new int[COLS];

    for( int i = 0; i < ROWS; i++)
    {
        for( int j = 0; j < COLS; j++)
        {
            for ( int k = 0; k < 3; k++)
            {
                a[i][j] += m1[i][k]*m2[k][j];
            }
        }
    }

    return a;
}



Thanks for any help in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Overloaded Operator for Matrix Multiplication

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Overloaded Operator for Matrix Multiplication

Posted 30 March 2010 - 11:42 AM

 a[i][j] += m1[i][k]*m2[k][j];


There is the problem. m1 and m2 are classes, so, m1 (which is an object)[i][j], seems logical to you? Maybe you meant to do this:
// access the double ptr a from m1 and m2 classes
a[i][j] += m1.a[i][k]*m2.a[k][j];


Was This Post Helpful? 2
  • +
  • -

#3 joesyuh  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 174
  • Joined: 30-September 08

Re: Overloaded Operator for Matrix Multiplication

Posted 30 March 2010 - 12:32 PM

That fixed my compilation errors and helped me better understand where my problem was. With all the operator overloading and crap I forgot that I am multiplying two distinct objects together, not simply two arrays from the same object.

Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1