Reputation: 1
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;
Upvotes: 0
Views: 283
Reputation: 210
You need to define Matrix before it's usage:
template<class T, size_t NROWS = 1, size_t NCOLS = 1>
class Matrix;
and add a return statement to operator<<, that returns os. You also do not need duplication of operator<< declaration, you can declare it only in a class body.
Upvotes: 1
Reputation: 13134
#include <cstddef>
#include <iostream>
template<typename, std::size_t, std::size_t> class Matrix;
template<typename T, std::size_t NROWS, std::size_t NCOLS>
std::ostream& operator<<(std::ostream &os, Matrix<T, NROWS, NCOLS> const &matrix)
{
for (std::size_t row{}; row < NROWS; ++row, os.put('\n'))
for (std::size_t col{}; col < NCOLS; ++col)
os << matrix.container[row][col] << ' ';
return os.put('\n');
}
template<typename T, std::size_t NROWS = 1, std::size_t NCOLS = 1>
class Matrix {
T container[NROWS][NCOLS] = {};
friend std::ostream& operator<< <>(std::ostream&, Matrix<T, NROWS, NCOLS> const&);
};
int main()
{
Matrix<float, 10, 5> mat;
std::cout << mat;
}
Please get rid of the C header <string.h>
.
Upvotes: 1
Reputation: 15234
If you uncomment out line 4, and change it as follows, the code you have compiles:
template <class t, size_t, size_t> class Matrix; //possible way of fixing the friend function.
It seems your problem is that the forward-declared Matrix template parameters don't match the Matrix definition that comes later.
Also, although the code will compile after this fix, there is still a warning which you probably also want to fix:
In function 'std::ostream& operator<<(std::ostream&, const Matrix<T, NROWS, NCOLS>&)':
31:1: warning: no return statement in function returning non-void [-Wreturn-type]
Upvotes: 3