0

I'm making simple matrix calculator for school project. I tried to use overloading operators but I have problem with operator "=". I checked it in debugger in Visual Studio and after assigning data from matrix A to matrix X all of data disappears. How should I repair this? Thank you in advance for your help and advice.

#include <iostream>
#include <vector>
#include <conio.h>
#include <stdlib.h>

using std::vector;

class Matrix {
private:
    int rows, cols;
    vector<vector<double>> matrix;

public:
    Matrix(int r = 0, int c = 0) : rows(r), cols(c) {
        std::vector<std::vector<double>> M(rows, std::vector<double>(cols));
        matrix = M;
    }
    ~Matrix() {}
    // void operator+(const Matrix &rhs);
    double get_rows() { return rows; }

    double get_cols() { return cols; }

    void set_matrix_value() {
        std::cout << "Put values in your matrix\n";

        for(int i = 0; i < matrix.size(); i++) {
            for(int j = 0; j < matrix[i].size(); j++) {
                // this->matrix[i][j] = int(_getch() - '0');
                std::cin >> matrix[i][j];
            }
        }
    }
    void display_matrix() {
        for(int i = 0; i < matrix.size(); i++) {
            std::cout << "|";
            for(int j = 0; j < matrix[i].size(); j++) {
                std::cout << matrix[i][j];
                if(j != matrix[i].size() - 1) {
                    std::cout << " ";
                }
            }
            std::cout << "|\n";
        }
    }
    Matrix operator=(const Matrix& M) {
        rows = M.rows;
        cols = M.cols;

        Matrix new_matrix(rows, cols);
        std::vector<std::vector<double>> m(rows, std::vector<double>(cols));
        new_matrix.matrix = m;
        for(int i = 0; i < m.size(); i++) {
            for(int j = 0; j < m[i].size(); j++) {
                new_matrix.matrix[i][j] = M.matrix[i][j];
            }
        }
        return new_matrix;
    }
    Matrix operator+(const Matrix& m) {
        if(rows != m.rows && cols != m.cols) {
            std::cout << "Matrix sizes do not match. Write martix again.";
            return (*this);
        }

        Matrix new_matrix(rows, cols);
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                new_matrix.matrix[i][j] = matrix[i][j] + m.matrix[i][j];
            }
        }
        return new_matrix;
    }
};

int main() {
    // int k;
    // int rows, cols;
    Matrix X;
    // std::cout << "Welcome in Matrix Mode!\n";
    X.display_matrix();
    Matrix A(3, 3);
    A.set_matrix_value();
    A.display_matrix();
    X = A;
    std::cout << X.get_rows() << " " << X.get_cols();
    X.display_matrix();
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Alex
  • 47
  • 5
  • 2
    The return type should not be "by value" - try `Matrix& operator= (const Matrix& M) ...`. Further, you shouldn't create a new `new_matrix` - you need to update `*this`: work on `matrix` (same as `this->matrix`) and not `net_matrix.matrix`. – Tony Delroy Feb 10 '21 at 22:27
  • 2
    The assignment operator should assign the values of the object passed in `M` to the object being assigned to `this`. Right now you don't even touch the object being assigned to. It looks like you're misunderstanding how the operator should function. – scohe001 Feb 10 '21 at 22:27
  • 5
    `Matrix` has [ownership](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) of no members incapable of copying or assigning themselves. Apply [the Rule of Zero](https://en.cppreference.com/w/cpp/language/rule_of_three): Do nothing. No destructor. No assignment operator. No copy constructor. – user4581301 Feb 10 '21 at 22:28
  • 1
    @TonyDelroy I did it just like you told me and now it's working. Thanks a lot! – Alex Feb 10 '21 at 22:38
  • @Alex: cool - thanks for letting us know. Cheers – Tony Delroy Feb 10 '21 at 22:42

0 Answers0