Multiplying two matrices

Keep getting error message: error C2678: binary '*' : no operator found which takes a left-hand operand of type 'Matrix' (or there is no acceptable conversion)... when trying to multiply matrices. Cannot find problem. Here is code the first code is all of my functions the second one is the main...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    #include <iostream>
    #include <cassert>
    #include "Matrix.h"
    
    using namespace std;
    
    Matrix::Matrix(int sizeX, int sizeY) : dx(sizeX), dy(sizeY)
    {
    	assert(sizeX > 0 && sizeY > 0);
    	p = new long*[dx];		
    				// create array of pointers to long integers
    	assert(p != 0);
    	for (int i = 0; i < dx; i++)
    	{	  // for each pointer, create array of long integers
    		p[i] = new long[dy];  
    		assert(p[i] != 0);
    		for (int j = 0; j < dy; j++)
    			p[i][j] = 0;
    	}
    }
    
    Matrix::~Matrix()
    {
    	for (int i = 0; i < dx; i++)
    		delete [] p[i];	// delete arrays of long integers
    	delete [] p;	// delete array of pointers to long
    }
    
    long &Matrix::Element(int x, int y)
    {
    	assert(x >= 0 && x < dx && y >= 0 && y < dy);
    	return p[x][y];
    }
    
    void Matrix::Print() const
    {
    	for (int x = 0; x < dx; x++)
    	{
    		for (int y = 0; y < dy; y++)
    			cout << p[x][y] << "\t";
    		cout << endl;
    	}
    }
    
    Matrix::Matrix(const Matrix &m) : dx(m.GetSizeX()), dy(m.GetSizeY())
    {
        assert(m.GetSizeX() > 0 && m.GetSizeY() > 0); // quits if m.GetSize(X)(Y) is < 0
        p = new long*[dx];
    				// create array of pointers to long integers
        assert(p != 0);
        for (int i = 0; i < dx; i++)
        {	  // for each pointer, create array of long integers
            p[i] = new long[dy];
            assert(p[i] != 0);
            for (int j = 0; j < dy; j++)
                p[i][j] = m.p[i][j];
        }
    }
    
    
    //Overloads the () operator to get the elment
    long& Matrix::operator()(int x, int y){
        return this->Element(x, y);
    }
    
    //overloads the assignment operator
    Matrix& Matrix::operator=(const Matrix &m){
        this->dx = m.dx;
        this->dy = m.dy;
        //going row by row, copying values
        for(int row = 0; row < this->dx; row++){
            for(int col = 0; col < this->dy; col++){
                this->p[row][col] = m.p[row][col];
            }
        }
        return *this;
    }
    
    //prints out the matrix
    ostream& operator<<(ostream &out, const Matrix &m){
        m.Print();
        return out;
    }
    
    //overloads the addition to add matries
    Matrix operator+(Matrix m1, Matrix m2){
        Matrix newMatrix(m1.dx, m1.dy);
        for(int row = 0; row < m1.dx; row++){
            for(int col = 0; col < m1.dy; col++){
                newMatrix.Element(row, col) = m1.p[row][col] + m2.p[row][col];
            }
        }
        return newMatrix;
    }
    
    //overloads the multiplication operator to multiply
    //	a matrix by a constant value
    Matrix operator*(int fac, Matrix m1){
        Matrix newMatrix(m1.dx, m1.dy);
        for(int row = 0; row < m1.dx; row++){
            for(int col = 0; col < m1.dy; col++){
                newMatrix.Element(row, col) = fac * m1.p[row][col];
            }
        }
        
        return newMatrix;
    }
    
    
    //overloads the multiplication operator to multiply
    //	a matrix by a constant value
    Matrix operator*(Matrix m1, int fac){
        Matrix newMatrix(m1.dx, m1.dy);
        for(int row = 0; row < m1.dx; row++){
            for(int col = 0; col < m1.dy; col++){
                newMatrix.Element(row, col) = fac * m1.p[row][col];
            }
        }
        return newMatrix;
    }
    
    ////overloads the multiplication operator to multiply
    //	a matrix by another matrix.
    Matrix operator*(Matrix m1, Matrix m2)
    {
        Matrix newMatrix(m1.dx, m2.dy);
        for(int i = 0; i < newMatrix.dx; i++){
            for(int j = 0; j < newMatrix.dy; j++){
                int sum = 0;
                for(int k = 0; k < m1.dx; k++){
                    sum += m1.p[k][j] * m2.p[i][k];
                }
                newMatrix.Element(i, j) = sum;
            }
        }
        return newMatrix;
    }



And here is main...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    #include <cstdlib>
    #include <iostream>
    #include <cassert>
    #include <ctime>
    #include "Matrix.h"
    
    using namespace std;
    
    int main(int argc, char** argv) {
        int size1, size2, size3;
        const int RANGE = 2; //using to generate random number in the range[0,1]
            
        cout << "Please input three positive integers to decide the sizes of matrices: ";
        cin >> size1 >> size2 >> size3;
        // Make sure each size is positive, otherwise abort the program
        assert(size1 > 0 && size2 > 0 && size3 > 0);
        
        // Declare Matrix objects with sizes passing in as parameter
        // The system automatically calls the constructor of Matrix class, which takes two parameters
        Matrix myMatrix1(size1, size2);
        
        // Simulate the adjacency matrix for undirected graph
        // Which only stores either 0 or 1 in the matrix
        // where 0 represents no path between two vertices in the grpah
        //       1 represents a path between two vertices in the graph
        srand(time(0));
        for (int i = 0; i < size1; i++)
            for (int j = 0; j < size2; j++)
                myMatrix1(i,j) = rand() % RANGE;
    
        Matrix yourMatrix1 = myMatrix1;
        Matrix hisMatrix1 = myMatrix1;
        Matrix herMatrix1 = myMatrix1;
        cout << "myMatrix1: " << endl;
        cout << myMatrix1 << endl << endl;
        
        cout << "yourMatrix1 = myMatrix1: " << endl;
        cout << yourMatrix1 << endl << endl;
    
        yourMatrix1 = 2 * myMatrix1;
        hisMatrix1 = myMatrix1 + yourMatrix1;
        herMatrix1 = myMatrix1 * 3;
        // hisMatrix1 and herMatrix1 should be identical now, but still independent
    
        cout << "myMatrix1: " << endl;
        cout << myMatrix1 << endl << endl;
        
        cout << "yourMatrix1 = 2 * myMatrix1: " << endl;
        cout << yourMatrix1 << endl << endl;
        
        cout << "hisMatrix1 = myMatrix1 + yourMatrix1: " << endl;
        cout << hisMatrix1 << endl << endl;
    
        cout << "herMatrix1 = myMatrix1 * 3: " << endl;
        cout << herMatrix1 << endl << endl;
    
        // herMatrix1 should be 4 * myMatrix1, and hisMatrix has not been changed
        herMatrix1 = herMatrix1 + myMatrix1;
        cout << "hisMatrix1: " << endl;
        cout << hisMatrix1 << endl << endl;
        cout << "herMatrix1 = herMatrix1 + myMatrix1: " << endl;
        cout << herMatrix1 << endl << endl;
    
        // Extra part of this Lab assignment
        // You need to define the matrix multiplication in your definition of class Matrix
        Matrix myMatrix2(size2,size3);
        Matrix yourMatrix2(size2, size1);
        Matrix hisMatrix2(size1, size3);
        Matrix herMatrix2(size1, size1);    
        for (int i = 0; i < size2; i++)
        {
            for (int j = 0; j < size3; j++)
                myMatrix2(i,j) = rand() % RANGE;
            for (int j = 0; j < size1; j++)
    	        yourMatrix2(i,j) = rand() % RANGE;
        }
        hisMatrix2 = myMatrix1 * myMatrix2;
        herMatrix2 = yourMatrix1 * yourMatrix2;
        cout << "myMatrix1: " << endl;
        cout << myMatrix1 << endl << endl;
        
        cout << "myMatrix2: " << endl;
        cout << myMatrix2 << endl << endl;
        
        cout << "hisMatrix2 = myMatrix1 * myMatrix2: " << endl;
        cout << hisMatrix2 << endl << endl;
        
        cout << "yourMatrix1: " << endl;
        cout << yourMatrix1 << endl << endl;
        
        cout << "yourMatrix2: " << endl;
        cout << yourMatrix2 << endl << endl;
        
        cout << "herMatrix2 = yourMatrix1 * yourMatrix2: " << endl;
        cout << herMatrix2 << endl << endl;
    
        cout << "herMatrix2's square: " << endl;
        cout << herMatrix2*herMatrix2 << endl << endl;    
    
        return 0;
    }


Sorry for all the code but I do not know where the problem is so I wanted to give as much information as possible...






closed account (48T7M4Gy)
Where is matrix.h ?
Here is matrix.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef MATRIX_H
#define	MATRIX_H

#include <iostream>

using namespace std;

class Matrix
{
  public:
    Matrix(int sizeX, int sizeY);
    Matrix(const Matrix &m);   // copy constructor 
    ~Matrix();
    int GetSizeX() const { return dx; }
    int GetSizeY() const { return dy; }
    long &Element(int x, int y);        // return reference to an element
    void Print() const;
    long &operator()(int x, int y);  // () operator overloading
    Matrix &operator=(const Matrix &m);  // = operator overloading
    friend ostream &operator<<(ostream &out, const Matrix &m); // << operator overloading
    friend Matrix operator+(Matrix m1, Matrix m2); // binary + operator overloading
    friend Matrix operator*(int fac, Matrix m1);   // binary * operator overloading
    friend Matrix operator*(Matrix m1, int fac);   // binary * operator overloading
	int dx, dy;
	long **p; 
        
    
};

#endif	/* MATRIX_H */
closed account (48T7M4Gy)
Your code compiles and runs. 3 digits I used were 2,3 and 4

What is not working. Show me some input and output

Please input three positive integers to decide the sizes of matrices: 2
3
4
myMatrix1: 
1	1	0	
1	0	1	


yourMatrix1 = myMatrix1: 
1	1	0	
1	0	1	


myMatrix1: 
1	1	0	
1	0	1	


yourMatrix1 = 2 * myMatrix1: 
2	2	0	
2	0	2	


hisMatrix1 = myMatrix1 + yourMatrix1: 
3	3	0	
3	0	3	


herMatrix1 = myMatrix1 * 3: 
3	3	0	
3	0	3	


hisMatrix1: 
3	3	0	
3	0	3	


herMatrix1 = herMatrix1 + myMatrix1: 
4	4	0	
4	0	4	


myMatrix1: 
1	1	0	
1	0	1	


myMatrix2: 
0	0	0	0	
0	0	0	0	
0	1	1	0	


hisMatrix2 = myMatrix1 * myMatrix2: 
0	0	0	0	
0	0	0	0	


yourMatrix1: 
2	2	0	
2	0	2	


yourMatrix2: 
1	0	
1	1	
1	1	


herMatrix2 = yourMatrix1 * yourMatrix2: 
2	2	
4	2	


herMatrix2's square: 
12	8	
16	12	


Program ended with exit code: 0
Last edited on
closed account (48T7M4Gy)
Well that was a show stopper.

Also operator * needs to be a friend in your setup.


You can also compare yours separately with this other one of mine that works:

friend Matrix operator+ (const Matrix&, const Matrix&);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//MULTIPLICATION
Matrix operator* ( const Matrix& m1, const Matrix& m2 )
{
    if (m1.m_nCols != m2.m_nRows) { throw std::invalid_argument("incompatible rows & cols"); }
    
    Matrix temp(m1.m_nRows, m2.m_nCols);
    
    for (int i = 0; i <  m1.m_nRows; i++)
    {
        for (int j = 0; j < m2.m_nCols; j++)
        {
            temp.m_dValue[i][j] = 0;
            for (int k = 0; k < m2.m_nRows; k++)
            {
                temp.m_dValue[i][j] += m1.m_dValue[i][k] * m2.m_dValue[k][j];
            }
        }
    }
    return temp;
}
Last edited on
Well it only compiles part of the program... and here is the error code i get...

Lab10extra.cpp: In function ‘int main(int, char**)’:
Lab10extra.cpp:84:28: error: no match for ‘operator*’ (operand types are ‘Matrix’ and ‘Matrix’)
hisMatrix2 = myMatrix1 * myMatrix2;
^
Lab10extra.cpp:84:28: note: candidates are:
In file included from Lab10extra.cpp:12:0:
Matrix.h:27:19: note: Matrix operator*(Matrix, int)
friend Matrix operator*(Matrix m1, int fac); // binary * operator overloading
^
Matrix.h:27:19: note: no known conversion for argument 2 from ‘Matrix’ to ‘int’
Matrix.h:26:19: note: Matrix operator*(int, Matrix)
friend Matrix operator*(int fac, Matrix m1); // binary * operator overloading
^
Matrix.h:26:19: note: no known conversion for argument 1 from ‘Matrix’ to ‘int’
Lab10extra.cpp:85:30: error: no match for ‘operator*’ (operand types are ‘Matrix’ and ‘Matrix’)
herMatrix2 = yourMatrix1 * yourMatrix2;
^
Lab10extra.cpp:85:30: note: candidates are:
In file included from Lab10extra.cpp:12:0:
Matrix.h:27:19: note: Matrix operator*(Matrix, int)
friend Matrix operator*(Matrix m1, int fac); // binary * operator overloading
^
Matrix.h:27:19: note: no known conversion for argument 2 from ‘Matrix’ to ‘int’
Matrix.h:26:19: note: Matrix operator*(int, Matrix)
friend Matrix operator*(int fac, Matrix m1); // binary * operator overloading
^
Matrix.h:26:19: note: no known conversion for argument 1 from ‘Matrix’ to ‘int’
Lab10extra.cpp:105:23: error: no match for ‘operator*’ (operand types are ‘Matrix’ and ‘Matrix’)
cout << herMatrix2*herMatrix2 << endl << endl;
^
Lab10extra.cpp:105:23: note: candidates are:
In file included from Lab10extra.cpp:12:0:
Matrix.h:27:19: note: Matrix operator*(Matrix, int)
friend Matrix operator*(Matrix m1, int fac); // binary * operator overloading
^
Matrix.h:27:19: note: no known conversion for argument 2 from ‘Matrix’ to ‘int’
Matrix.h:26:19: note: Matrix operator*(int, Matrix)
friend Matrix operator*(int fac, Matrix m1); // binary * operator overloading
^
Matrix.h:26:19: note: no known conversion for argument 1 from ‘Matrix’ to ‘int’


It will not run the part where it multiplies matrices. So will what you told me above fix this? I am a little confused on what you put in your previous answer.
closed account (48T7M4Gy)
I combined all your 3 separate components to save messing around. It is a straight cut and paste of your stuff and I ran it in the latest version of XCode.

I'll put a copy here shortly. I'd suggest you run it on your system. The reason is line numbers are critical to pinpointing wher the error is on yours.

The other way which might suit you is show us what line the errors occur on, starting with the first and using the programs you have posted here already.

Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef MATRIX_H
#define	MATRIX_H

#include <iostream>

using namespace std;

class Matrix
{
public:
    Matrix(int sizeX, int sizeY);
    Matrix(const Matrix &m);   // copy constructor
    ~Matrix();
    int GetSizeX() const { return dx; }
    int GetSizeY() const { return dy; }
    long &Element(int x, int y);        // return reference to an element
    void Print() const;
    long &operator()(int x, int y);  // () operator overloading
    Matrix &operator=(const Matrix &m);  // = operator overloading
    friend ostream &operator<<(ostream &out, const Matrix &m); // << operator overloading
    friend Matrix operator+(Matrix m1, Matrix m2); // binary + operator overloading
    friend Matrix operator*(int fac, Matrix m1);   // binary * operator overloading
    friend Matrix operator*(Matrix m1, int fac);   // binary * operator overloading
    
    int dx, dy;
    long **p;
    
    
};

#endif	/* MATRIX_H */

#include <iostream>
#include <cassert>
//#include "Matrix.h"

using namespace std;

Matrix::Matrix(int sizeX, int sizeY) : dx(sizeX), dy(sizeY)
{
    assert(sizeX > 0 && sizeY > 0);
    p = new long*[dx];
    // create array of pointers to long integers
    assert(p != 0);
    for (int i = 0; i < dx; i++)
    {	  // for each pointer, create array of long integers
        p[i] = new long[dy];
        assert(p[i] != 0);
        for (int j = 0; j < dy; j++)
            p[i][j] = 0;
    }
}

Matrix::~Matrix()
{
    for (int i = 0; i < dx; i++)
        delete [] p[i];	// delete arrays of long integers
    delete [] p;	// delete array of pointers to long
}

long &Matrix::Element(int x, int y)
{
    assert(x >= 0 && x < dx && y >= 0 && y < dy);
    return p[x][y];
}

void Matrix::Print() const
{
    for (int x = 0; x < dx; x++)
    {
        for (int y = 0; y < dy; y++)
            cout << p[x][y] << "\t";
        cout << endl;
    }
}

Matrix::Matrix(const Matrix &m) : dx(m.GetSizeX()), dy(m.GetSizeY())
{
    assert(m.GetSizeX() > 0 && m.GetSizeY() > 0); // quits if m.GetSize(X)(Y) is < 0
    p = new long*[dx];
    // create array of pointers to long integers
    assert(p != 0);
    for (int i = 0; i < dx; i++)
    {	  // for each pointer, create array of long integers
        p[i] = new long[dy];
        assert(p[i] != 0);
        for (int j = 0; j < dy; j++)
            p[i][j] = m.p[i][j];
    }
}


//Overloads the () operator to get the elment
long& Matrix::operator()(int x, int y){
    return this->Element(x, y);
}

//overloads the assignment operator
Matrix& Matrix::operator=(const Matrix &m){
    this->dx = m.dx;
    this->dy = m.dy;
    //going row by row, copying values
    for(int row = 0; row < this->dx; row++){
        for(int col = 0; col < this->dy; col++){
            this->p[row][col] = m.p[row][col];
        }
    }
    return *this;
}

//prints out the matrix
ostream& operator<<(ostream &out, const Matrix &m){
    m.Print();
    return out;
}

//overloads the addition to add matries
Matrix operator+(Matrix m1, Matrix m2){
    Matrix newMatrix(m1.dx, m1.dy);
    for(int row = 0; row < m1.dx; row++){
        for(int col = 0; col < m1.dy; col++){
            newMatrix.Element(row, col) = m1.p[row][col] + m2.p[row][col];
        }
    }
    return newMatrix;
}

//overloads the multiplication operator to multiply
//	a matrix by a constant value
Matrix operator*(int fac, Matrix m1){
    Matrix newMatrix(m1.dx, m1.dy);
    for(int row = 0; row < m1.dx; row++){
        for(int col = 0; col < m1.dy; col++){
            newMatrix.Element(row, col) = fac * m1.p[row][col];
        }
    }
    
    return newMatrix;
}


//overloads the multiplication operator to multiply
//	a matrix by a constant value
Matrix operator*(Matrix m1, int fac){
    Matrix newMatrix(m1.dx, m1.dy);
    for(int row = 0; row < m1.dx; row++){
        for(int col = 0; col < m1.dy; col++){
            newMatrix.Element(row, col) = fac * m1.p[row][col];
        }
    }
    return newMatrix;
}

////overloads the multiplication operator to multiply
//	a matrix by another matrix.
Matrix operator*(Matrix m1, Matrix m2)
{
    Matrix newMatrix(m1.dx, m2.dy);
    for(int i = 0; i < newMatrix.dx; i++){
        for(int j = 0; j < newMatrix.dy; j++){
            int sum = 0;
            for(int k = 0; k < m1.dx; k++){
                sum += m1.p[k][j] * m2.p[i][k];
            }
            newMatrix.Element(i, j) = sum;
        }
    }
    return newMatrix;
}

#include <cstdlib>
#include <iostream>
#include <cassert>
#include <ctime>
//#include "Matrix.h"

using namespace std;

int main(int argc, char** argv) {
    int size1, size2, size3;
    const int RANGE = 2; //using to generate random number in the range[0,1]
    
    cout << "Please input three positive integers to decide the sizes of matrices: ";
    cin >> size1 >> size2 >> size3;
    // Make sure each size is positive, otherwise abort the program
    assert(size1 > 0 && size2 > 0 && size3 > 0);
    
    // Declare Matrix objects with sizes passing in as parameter
    // The system automatically calls the constructor of Matrix class, which takes two parameters
    Matrix myMatrix1(size1, size2);
    
    // Simulate the adjacency matrix for undirected graph
    // Which only stores either 0 or 1 in the matrix
    // where 0 represents no path between two vertices in the grpah
    //       1 represents a path between two vertices in the graph
    srand(time(0));
    for (int i = 0; i < size1; i++)
        for (int j = 0; j < size2; j++)
            myMatrix1(i,j) = rand() % RANGE;
    
    Matrix yourMatrix1 = myMatrix1;
    Matrix hisMatrix1 = myMatrix1;
    Matrix herMatrix1 = myMatrix1;
    cout << "myMatrix1: " << endl;
    cout << myMatrix1 << endl << endl;
    
    cout << "yourMatrix1 = myMatrix1: " << endl;
    cout << yourMatrix1 << endl << endl;
    
    yourMatrix1 = 2 * myMatrix1;
    hisMatrix1 = myMatrix1 + yourMatrix1;
    herMatrix1 = myMatrix1 * 3;
    // hisMatrix1 and herMatrix1 should be identical now, but still independent
    
    cout << "myMatrix1: " << endl;
    cout << myMatrix1 << endl << endl;
    
    cout << "yourMatrix1 = 2 * myMatrix1: " << endl;
    cout << yourMatrix1 << endl << endl;
    
    cout << "hisMatrix1 = myMatrix1 + yourMatrix1: " << endl;
    cout << hisMatrix1 << endl << endl;
    
    cout << "herMatrix1 = myMatrix1 * 3: " << endl;
    cout << herMatrix1 << endl << endl;
    
    // herMatrix1 should be 4 * myMatrix1, and hisMatrix has not been changed
    herMatrix1 = herMatrix1 + myMatrix1;
    cout << "hisMatrix1: " << endl;
    cout << hisMatrix1 << endl << endl;
    cout << "herMatrix1 = herMatrix1 + myMatrix1: " << endl;
    cout << herMatrix1 << endl << endl;
    
    // Extra part of this Lab assignment
    // You need to define the matrix multiplication in your definition of class Matrix
    Matrix myMatrix2(size2,size3);
    Matrix yourMatrix2(size2, size1);
    Matrix hisMatrix2(size1, size3);
    Matrix herMatrix2(size1, size1);
    for (int i = 0; i < size2; i++)
    {
        for (int j = 0; j < size3; j++)
            myMatrix2(i,j) = rand() % RANGE;
        for (int j = 0; j < size1; j++)
            yourMatrix2(i,j) = rand() % RANGE;
    }
    hisMatrix2 = myMatrix1 * myMatrix2;
    herMatrix2 = yourMatrix1 * yourMatrix2;
    cout << "myMatrix1: " << endl;
    cout << myMatrix1 << endl << endl;
    
    cout << "myMatrix2: " << endl;
    cout << myMatrix2 << endl << endl;
    
    cout << "hisMatrix2 = myMatrix1 * myMatrix2: " << endl;
    cout << hisMatrix2 << endl << endl;
    
    cout << "yourMatrix1: " << endl;
    cout << yourMatrix1 << endl << endl;
    
    cout << "yourMatrix2: " << endl;
    cout << yourMatrix2 << endl << endl;
    
    cout << "herMatrix2 = yourMatrix1 * yourMatrix2: " << endl;
    cout << herMatrix2 << endl << endl;
    
    cout << "herMatrix2's square: " << endl;
    cout << herMatrix2*herMatrix2 << endl << endl;
    
    return 0;
}
closed account (48T7M4Gy)
That is your code and it compiles without any errors.
closed account (48T7M4Gy)
In your matrix.h you are missing

friend Matrix operator*(Matrix m1, Matrix m2);

That's why my composite works and yours doesn't as the error messages indicate.

Once that is fixed you need to check that it is multiplying the matrices correctly. I suspect not but I haven't checked thoroughly.
Last edited on
Thank you so much! It worked perfectly!
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.