Overloading << operator to print the elements of a matrix in a template class

Overloading << operator to print the elements of a matrix in a template class

why am I having an error on the operator '[]'. I wanted to print the contents of my matrix. If i can't use the brackets, what could i do then?

why am I having an error on the operator '[]'. I wanted to print the contents of my matrix. If i can't use the brackets, what could i do then?

here's a sample of the code:

#include <iostream>
#include <vector>

template <typename T> class Matrix { private: int m; int n; std::vector<T> x; std::vector<std::vector<int>> Mat;

public:
Matrix (const unsigned int &amp;m, const unsigned int &amp;n, std::vector&lt;T&gt; x);
Matrix(const Matrix &amp;M);
Matrix&lt;T&gt; operator = (const Matrix &amp;M);
// Matrix&lt;T&gt; operator [](const int &amp;index);

friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; os, const Matrix&lt;T&gt; &amp;M) {
    os &lt;&lt; "[";
    for (int i = 0; i&lt; M.m; i++){
        for (int j = 0; j&lt; M.n; j++){
            os &lt;&lt; M[i][j] &lt;&lt; ' ';
        }
        os &lt;&lt; '\n';
    }
    os &lt;&lt; "]\n";
    return os;
}

};

I have fixed the errors. But it doesn't print my matrix. This is my main:

int main(){
    std::vector<int> x = {1,2,3,4};
    Matrix<int> A{2,2,x};
    Matrix<int> B{2,2,x};
    std::cout << A;
    std::cout << B;
    return 0;
}

And this is my constructor, I needed to make a matrix from a vector where I specify the rows and columns.

template <typename T>
    Matrix<T>::Matrix (const unsigned int &m, const unsigned int &n, std::vector<T> x){ //constructor
    this -> m = m;
    this -> n = n;
    this -> x = x;

int index = 0;
for (int i = 0; i&lt;m; i++){
    for (int j = 0; j&lt;n; j++){
         Mat[i][j] = x[index];
        index++;
    }
}

}


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

C/C++ vs. Rust: A developer’s perspective

In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective

Variable Introduction in C#[Bangla]

LIKE | COMMENT | SHARE | SUBSCRIBE A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...