0

I am trying to make matrix structure with overloading operators '+' and '='.

Matrix structure:

struct Matrix{
    int rows;
    int cols;
    std::vector<int> data;
    Matrix(int rows, int cols, int val);    //creates matrix row*col and fills every position with val
    Matrix(const Matrix &m);    //copy constructor
    Matrix(int rowcol = 0);    //creates matrix rowcol*rowcol filled with zeros
    Matrix & operator=(const Matrix &m);
    void printMatrix();    //prints the matrix
    ...
};

Operators:

Matrix & Matrix::operator=(const Matrix &m) //assings matrix m to the new matrix
{

}

Matrix operator+(const Matrix &a, const Matrix &b) //sums two matrices
{
    //I know how to make the algorithm for summing two matrices, however i don´t know how
    //to return the result into existing matrix
}

And main function:

int main(){
    Matrix A(3,3,1);
    Matrix B(3,3,2);
    Matrix C(3,3,0);

    C = A + B;
    C.printMatrix();
    ...

    return 0;
}

I don´t know how to make it work. I tried something like this for the '=' operator:

Matrix & Matrix::operator=(const Matrix &m)
{
    static matrix d(m);    //using copy constructor
    return d;
}

but it didn´t work. It created new matrix with dimensions 0x0. Any advice on how to implement it?

Tautomer
  • 3
  • 1
  • 1
    Does this answer your question? [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – Richard Critten Mar 28 '20 at 17:00
  • 2
    I see nothing in the shown matrix class that requires overloading the copy constructor and the assignment operator. You will find that, as shown, copying an instance of the class will work, as if by magic. Welcome to C++! – Sam Varshavchik Mar 28 '20 at 17:07
  • [Some reading on Sam's point](https://en.cppreference.com/w/cpp/language/rule_of_three). Of particular interest if the Rule of Zero, but it's good to understand how and when to apply the other two rules, so you should read the whole thing. – user4581301 Mar 28 '20 at 17:12
  • A note on the attempted assignment operator. You almost discovered the [Copy and Swap idiom](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom). Where you went wrong is the assignment operator should assign to and return `this`, not a local object, `static` or otherwise. – user4581301 Mar 28 '20 at 17:16

1 Answers1

0

A working way to implement that operator= would be as follows:

Matrix& Matrix::operator=(const Matrix &m)
{
    this->rows = m.rows;
    this->cols = m.cols;
    this->data = m.data;

    return *this;
}

I won't approach the algorithm needed for operator+, but the surrounding machinery is also surprisingly simple:

Matrix operator+(const Matrix &a, const Matrix &b) //sums two matrices
{
    Matrix result(<num of rows>, <num of cols>, 0);

    // do things with result here, using your algorithm, a, and b
    // e.g. result.data[?] = a.data[?] ? b.data[?]

    return result;
}

To make this easier, since your data is one-dimensional (good! this is efficient!), you may want to implement an accessor that takes x and y dimensions and returns an index into data. For example:

int& Matrix::val(const int x, const int y)
{
    assert(x < cols);
    assert(y < rows);

    return data[x + y*cols];
}

The result of this may be both read and written, like this:

result.val(x1, y1) = a.val(x2, y2) + b.val(x3, y3);

Where those co-ordinates and the + are just things I made up, and should come from your algorithm.


If you also want an operator+=, to affect an existing matrix, then:

Matrix& Matrix::operator+(const Matrix &b) //sums two matrices
{
    // do things with your object here, using your algorithm and b

    return *this;
}

Where I return *this for the member operators, that's not strictly necessary, though it is conventional, and it allows for easy chaining of operations (if you're into that kind of thing). You can find more information on operator overloading in your book.

Asteroids With Wings
  • 16,164
  • 2
  • 17
  • 33