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 &m, const unsigned int &n, std::vector<T> x); Matrix(const Matrix &M); Matrix<T> operator = (const Matrix &M); // Matrix<T> operator [](const int &index); friend std::ostream& operator << (std::ostream& os, const Matrix<T> &M) { os << "["; for (int i = 0; i< M.m; i++){ for (int j = 0; j< M.n; j++){ os << M[i][j] << ' '; } os << '\n'; } os << "]\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<m; i++){ for (int j = 0; j<n; j++){ Mat[i][j] = x[index]; index++; } }`

}

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

ＬＩＫＥ | ＣＯＭＭＥＮＴ | ＳＨＡＲＥ | ＳＵＢＳＣＲＩＢＥ A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a speci...