I have some code that is supposed to overload the operator << and be able to print a matrix object, but for some reason, it fails. Here's the code for it: (Matirx.h file)
#pragma once
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
template <int N=1, int M=1, class T = int>
class Matrix {
int rows;
int cols;
T matrix[N][M];
public:
Matrix(T matrixVal = 0) :rows(N), cols(M) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
this->matrix[i][j] = matrixVal;
}
}
}
int getRows() { return rows; }
int getCols() { return cols; }
T** getMatrix() { return matrix; }
friend ostream& operator<<(ostream& out, const Matrix<>& mat) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
out << this->matrix[i][j];
out << " ";
}
out << endl;
}
return out;
}
But when I add it, I get these errors:
Error C2084 function 'std::ostream &Matrix<4,4,int>::operator <<(std::ostream &,const Matrix<1,1,int> &)' already has a body
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'Matrix<4,4,int>' (or there is no acceptable conversion)
Error (active) E0349 no operator "<<" matches these operands
here's the main code:
#include "matrix.h"
template <int row, int col, typename T>
void printDiag(Matrix<row, col, T>& mat) {
int number;
T* diag = mat.getDiag(number);
for (int i = 0; i < number; i++)
{
std::cout << diag[i] << " ";
}
std::cout << std::endl;
delete[] diag;
}
int main() {
//freopen("output_matrix.txt", "w", stdout);
Matrix<4, 4> mat;
std::cout << mat << std::endl;
Matrix<4, 4> identity(1);
std::cout << identity << std::endl;
any help would be appreciated (: