i have a class:
template<class T>
class matrix
{
private:
int COLS,ROWS;
public:
inline matrix(int r,int c){
this->COLS=r;
this->ROWS=c;
}
template<class T2>
friend ostream& info(ostream& os);
};
i tried in so many ways to implement the info function.but none is succeed.
i want to use it in main function
Matrix<int> M(10,20);
cout<<info<<M;
i want to out put the Matrix class's cols, and rows information.
i have tried so many time in implementing the friend class info, but failed.
any one can tell me how can do it?
sorry, i forget to put the << overloading part.
template<class T2>
friend ostream& operator<< (ostream &out,matrix<T2> &cMatrix);
the implementation:
template<class T2>
ostream & operator<<(ostream &out, matrix<T2> &cMatrix) {
out<<cMatrix.getCOLS();// sorry for didn't put the get function, it's not easy to put code line by line here.
out<<cMatrix.getROWS();
return out;
}
my << operate works fun.
but when i want to use info, i got errors.
i am not sure, how to implement the own type manipulators as a friend function. i google some, but they are not friend function. and also, it is a kind of template funtion.
here is what i am tring:
template<class T2>
ostream& info(ostream& os,matrix<T2> &cMatrix)
{
int cols=cMatrix.getCOLS();
int rows=cMatrix.getROWS();
os<<rols<<"X"<<rows<<" matrix "<<endl;
return os;
}