0

My homework question was:

  • Create a matrix class using the template and overload +,-,,/,<<,>>,+=,-=,=,/= operators so they can be used for matrices operations.

  • I've overloaded the << and >> operators so I can read the matrix elements and print the matrix, and seems to work alright but when I tried to overload the + operator I've got a segmentation fault and I don't know where the problem might be.

  • Matrix class:

    template <class T> class matrice{
    int l,c;
    vector<vector<T> > matrix;
public:
    matrice(){}
    ~matrice(){}

    matrice operator+(matrice altaMatrice){...}
    matrice operator-(matrice altaMatrice){...}
    matrice operator*(matrice altaMatrice){...}
    matrice operator/(matrice altaMatrice){...}

    matrice operator+=(matrice altaMatrice){...}
    matrice operator-=(matrice altaMatrice){...}
    matrice operator*=(matrice aaltaMatrice){...}
    matrice operator/=(matrice altaMatrice){...}

    friend istream& operator>>(istream& myStream, matrice& altaMatrice){...}
    friend ostream& operator<<(ostream& myStream, matrice& altaMatrice){...}
};
  • overloading >> and <<:
        friend istream& operator>>(istream& myStream, matrice& altaMatrice){
        T element;
        cout<<"linii si coloane:"<<endl;
        cin >> altaMatrice.l;
        cin >> altaMatrice.c;

        cout<<"elem matrice"<<endl;
        for (int i = 0; i < altaMatrice.l; i++) {
            vector<T> linie;
            for (int j = 0; j < altaMatrice.c; j++) {
                cin >> element;
                linie.push_back(element);
            }
            altaMatrice.matrix.push_back(linie);
        }
        return myStream;
    }


        friend ostream& operator<<(ostream& myStream, matrice& altaMatrice){
        cout << "\n\n";
        for (int i = 0; i < altaMatrice.l; i++) {
            for (int j = 0; j < altaMatrice.c; j++)
                cout << altaMatrice.matrix[i][j] << "\t";
            cout << "\n\n";
        }
        return myStream;
}
  • and here is the overloading I have tried and got the segmentation fault:
    matrice operator+(matrice altaMatrice){
        matrice temp;
        for(int i=0; i<altaMatrice.l; i++)
        {
            for(int j=0; j<altaMatrice.c; j++)
            {
                temp.matrix[i][j] = this->matrix[i][j] + altaMatrice.matrix[i][j];
            }
        }
        return temp;
    }

-PS: I don't know if it helps but this is the main function:

int main(){
    matrice<int> A;
    cin >> A;
    cout << A;

    matrice<int> C;
    C = A + A;
    cout << C;
    
    return 0;
}
  • Here is the output without the matrix C, only reading and printing the matrix A:
linii si coloane:
2
2
elem matrice
1
2
3
4


1       2

3       4


Process finished with exit code 0
  • the output with the matrix C (A + A):
linii si coloane:
2
2
elem matrice
1
2
3
4

1       2

3       4

Process finished with exit code -1073741819 (0xC0000005)
  • When you do `matrice temp;`, how many elements will `temp.matrix` contain? – Lukas-T Jan 29 '21 at 09:58
  • `temp.matrix[i][j] = this->matrix[i][j] + altaMatrice.matrix[i][j];` 1) `temp.matrix` is empty `std::vector`, hence `operator[]`, with any value, can't be used. 2) Loops are defined as `for(int i=0; il == altaMatrice.l`? No checks are performed, and this is yet another possibility to go out of bounds of one of the `std::vector`? – Algirdas Preidžius Jan 29 '21 at 09:59
  • Functions like `matrice operator+=(matrice altaMatrice){...}` should return a reference to `*this`, not a copy: `matrice& operator+=(const matrice& altaMatrice){...; return *this; }` - Note `matrice&` – Ted Lyngmo Jan 29 '21 at 10:00
  • [OT]: You might add some `const`, and pass parameter b const reference instead of by copy. – Jarod42 Jan 29 '21 at 10:00
  • The `temp.matrix` should contain the same number of elements as the `this->matrix` and `altaMatrice.matrix`. – SergiuGeorge Jan 29 '21 at 10:05
  • @SergiuGeorge But why? The `[]`-operator does _not_ increase the size of the matrix. In `operator>>` you correctly use `push_back`. – Lukas-T Jan 29 '21 at 10:07
  • Also: `friend ostream& operator<<(ostream& myStream, matrice& altaMatrice);` should be `friend ostream& operator<<(ostream& myStream, const matrice& altaMatrice);` – Ted Lyngmo Jan 29 '21 at 10:17
  • Your `operator+()` needs to assign values to `temp.l` and `temp.c`. Otherwise they are whatever is there are initialised to in the default constructor. Also, there is no operation in the `operator+()` that resizes `temp.matrix` OR the elements of `temp.matrix` BEFORE assigning values to the elements. That causes undefined behaviour. – Peter Jan 29 '21 at 10:18

0 Answers0