I've been stuck on this code for a while and can't get it to compile, what exactly am I doing wrong? If there are bugs present when it compiles, please ignore them as I can fix that on my own. As of right now I'm just trying to get it to run. Thank you in advance.
#include <iostream>
#include <string.h>
//template <class t> class Matrix; //possible way of fixing the friend function.
using namespace std;
template<class T, size_t NROWS, size_t NCOLS>
std::ostream & operator<<(std::ostream &os, const Matrix<T,NROWS, NCOLS> &matrix);
template<class T, size_t NROWS = 1, size_t NCOLS = 1>
class Matrix{
public:
Matrix(){}
friend std::ostream &operator<< <>(std::ostream&os,const Matrix<T, NROWS, NCOLS> &matrix);
private:
T container[NROWS][NCOLS];
};
template<class T,size_t NROWS, size_t NCOLS>
std::ostream &operator<<(std::ostream &os,const Matrix<T,NROWS,NCOLS>&matrix){
for(size_t i=0;i<NROWS;++i){
for(size_t j=0;j<NCOLS;++j){
os <<matrix.container[i][j]<<" ";
}
os <<std::endl;
}
os <<std::endl;
}
int main(){
Matrix<float, 10, 5> mat;
cout << mat;
return 0;
}
The errors from the IDE I use are as follows:
main.cpp:8:51: error: no template named 'Matrix' std::ostream & operator<<(std::ostream &os, const Matrix &matrix);
main.cpp:15:24: error: no function template matches function template specialization 'operator<<' friend std::ostream &operator<< <>(std::ostream&os,const Matrix &matrix);
main.cpp:35:32: note: in instantiation of template class 'Matrix' requested here Matrix mat;