mardi 14 septembre 2021

How to overload ostream operator in a C ++ struct? [duplicate]

How can I overload ostream operator in a C ++ struct?

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
template<class T, int N> struct Matrix {
    typedef Matrix M;
    array<array<T, N>, N> d{};
    M operator*(const M& m) const {
        M a;
        rep(i,0,N) rep(j,0,N)
            rep(k,0,N) a.d[i][j] += d[i][k]*m.d[k][j];
        return a;
    }
    std::ostream &operator<<(std::ostream &output)
    {
        rep(i,0,N){
            rep(j,0,N)
                output<< d[i][j] <<" ";
            output<<endl;
        }
        return output;
    }
};

signed main(int argc, const char** argv) {
    Matrix<int, 3> A;
    A.d = , 4, 7}};
    // cout << A ;
    A << cout ;
    return 0;
}

Error : no operator "<<" matches these operands -- operand types are: std::ostream << Matrix<int, 3>C/C++(349)

Aucun commentaire:

Enregistrer un commentaire