### Help with a class

Im supposed to write a class to do various matrix operations. The class has a 2d array to represent the matrix. It uses an add method to add 2 matrices together. The method takes a matrix as it's argument and returns a matrix.

Ignore the first 2 constructors as they have no relation to the methods.
The error is in the add method when i try to use the Matrix argument. I have commented the program for reference. The error only shows up when i use anotherMatrix[i][j].

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188`` ``````#include #include #include #include using namespace std; // The Matrix class definition goes here class Matrix { public: //Constructors Matrix(); Matrix(int, int); Matrix(int[][5], int, int); //Methods void print(); Matrix add( Matrix ); Matrix subtract( Matrix ); //counter variables int i; int j; private: //Constants for maximum array size of 5 #define maxRows 5 #define maxCols 5 //The matrix for the class int matrixArray[maxRows][maxCols]; //Variables to represent the actual array size (most arrays will not be maximum size) int actualRows, actualCols; }; //Constructor 1 sets the matrix size to the maximum and initializes it to 0's Matrix::Matrix() { actualRows=maxRows; actualCols=maxCols; // matrixArray[actualRows][actualCols]={0}; for(i=0;iMatrix::i] aMatrix[i][j]=anotherMatrix[i][j]+matrixArray[i][j]; } } } ``````
Last edited on
Like the error says: class Matrix does not have operator[]. If it would have, then the probable error would be that the return type of Matrix::operator[] (size_t) does not have operator[].

Just try in main
 ``12`` ``````Matrix m42; cout << m42[0];``````

Within add() you can get around the error because it is a member function and has therefore access to members:
`aMatrix[i][j] = anotherMatrix.matrixArray[i][j] + this->matrixArray[i][j];`

1. this and anotherMatrix might be of different size.
2. int aMatrix[3][3];
3. no return of value
Thanks for the quick reply. I will try what you suggested.

As for the last 3 things I know about them and will be fixing them.
I am having trouble returning the matrix back to the main function. In the info from the prof it states: "This method adds a Matrix object to another Matrix object. It takes a Matrix object as its argument and returns a Matrix object."

I have done searching and everywhere i go i am told returning a 2d array is not possible. This line of code was supplied by the prof: m5 = m3.add( m4 );
It indicates that after the member is called a matrix array should be returned to m5. But I cant seem to do this.
You have to create a Matrix object in the function and return that one object.
 ``123456`` ``````Matrix foo() { Matrix m42; // fill m42 with meaningful data return m42; }``````

Topic archived. No new replies allowed.