samedi 16 février 2019

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?

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;
    }
};

Aucun commentaire:

Enregistrer un commentaire