I am assuming that you are declaring a class template Matrix
that has a type argument T
, and that you want to use the defined operator>>
(you should make this more explicit in the question):
template <typename T>
class Matrix {
int rows, cols;
T* elements;
public:
Matrix( int c, int r ); // Do you really want the number of
// rows/columns to be of type `T`??
// Note: removed template, you only want to befriend (and define)
// a single operator<< that takes a Matrix<T> (the <T> is optional
// inside the class braces
friend std::istream& operator>>( std::istream& i, Matrix& m )
{
// m.rows, m.cols and m.elements is accessible here.
return i;
}
};
And then it is quite simple to use:
Matrix<double> m( 1, 2 );
std::cin >> m;
That is not the only option, it is just the most common one. That is in general a class template can befriend a single (non-templated) function (think operator<<
and operator>>
as functions) as in the code above, or it might want to befriend a function template (all instantiations) or a particular instantiation of a function template.
I wrote a long explanation of the different options and behaviors here, and I recommend you to read it.