Я застрял в этом коде некоторое время и не могу заставить его скомпилироваться, что именно я делаю не так? Если при компиляции присутствуют ошибки, пожалуйста, игнорируйте их, так как я могу это исправить самостоятельно. На данный момент я просто пытаюсь заставить его работать. Заранее спасибо.
#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;
}
Ошибки из IDE, которые я использую, следующие:
main.cpp: 8: 51: ошибка: нет шаблона с именем 'Matrix'
std :: ostream & operator << (std :: ostream & os, const Matrix & matrix); </p>
main.cpp: 15: 24: ошибка: шаблон не соответствует шаблону функции специализации 'operator <<' friend std :: ostream & operator << <> (std :: ostream & os, const Matrix & matrix);
main.cpp: 35: 32: note: при создании экземпляра класса шаблона 'Matrix', запрашиваемого здесь Matrix mat;