Question about Template class operator overloading (Absolute Beginner)

Here is my source code. It's a simple matrix class
I want to implement + operator overloading for matrix addition.
But this code is not work. I have no idea what to do at all.
Entire designing is wrong for this?

(Sorry for poor english, I'm not a native)

#include <iostream>

template<typename T, int r, int c>
struct Mat {
T arr[r][c];

void print_all() {
for (int i = 0; i < r ; i++) {
for (int j = 0; j < c ; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << '\n';
}
}

void get_rows_count() {
return r;
}

void get_columns_count() {
return c;
}

void print_rows_values(int rows_index) {
for (int i = 0; i < c ; i++) {
std::cout << arr[rows_index][i] << ' ';;
}
std::cout << '\n';
}

void print_columns_values(int columns_index) {
for (int i = 0; i < r ; i++) {
std::cout << arr[i][columns_index] << '\n';;
}
}

// how can I modify below?
Mat operator+(Mat& right) {
Mat<T, r, c> temp;
for (int i = 0; i < r ; i++) {
for (int j = 0; j < c ; j++) {
temp.arr[i][j] = arr[i][j] + right[i][j];
}
}
return temp;
}
};


int main() {
Mat<float, 2, 3> m; // It's ok

m = { 1, 2, 8, 9, 5, 6 }; // It's ok

m.print_all(); // It's ok

auto temp = m + m; // problem here.

std::cin.get();
}
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
#include <iostream>

template < typename T, std::size_t NROWS, std::size_t NCOLS >
struct mat
{
    T arr[NROWS][NCOLS] ;

    static constexpr std::size_t row_count() { return NROWS ; }
    static constexpr std::size_t col_count() { return NCOLS ; }

    void print_row( std::size_t row ) const
    {
        if( row < row_count() )
        {
            for( const T& v : arr[row] ) std::cout << v << ' ' ;
            std::cout << '\n' ;
        }
    }

    void print() const
    { for( std::size_t row = 0 ; row < row_count() ; ++row ) print_row(row) ; }

    mat& operator+= ( const mat& right )
    {
        for( std::size_t row = 0 ; row < row_count() ; ++row )
            for( std::size_t col = 0 ; col < col_count() ; ++col )
                arr[row][col] += right.arr[row][col] ;

        return *this ;
    }

    // Since for every binary arithmetic operator there exists
    // a corresponding compound assignment operator,
    // canonical forms of binary operators are implemented
    // in terms of their compound assignments
    // see: https://en.cppreference.com/w/cpp/language/operators#Binary_arithmetic_operators
    friend mat operator+ ( mat left, const mat& right ) // note: left is passed by value
    { return left += right ; } // reuse compound assignment, return the result by value
};

int main()
{
    const mat<int,2,3> a { 1,2,3, 4,5,6 } ;
    const mat<int,2,3> b { 10,20,30, 40,50,60 } ;

    const auto c = a + b ;
    c.print() ;
}

http://coliru.stacked-crooked.com/a/c276a8f241714ea2
Thank you very much!, JLBorges. I will try to understand that code. Thank you.
Last edited on
Topic archived. No new replies allowed.