hey guys,
i’m confused as to how to access an overloaded template function like this one:
template <typename T>
friend istream& operator>> (istream& in, Matrix& right)
{
for(int i=0; i<right.rows*right.cols; i++)
cin >> right.elements[i];
}
with a function such as:
template <typename T>
Matrix(T r, T c) {rows=r; cols=c; elements=new T[r*c];}
i was able to do
Matrix <double> test(number, number)
for example, but i have no idea how to use a templated >> operator (or << or * or +..) any help would be appreciated. thanks!
I am assuming that you are declaring a class template
Matrixthat has a type argumentT, and that you want to use the definedoperator>>(you should make this more explicit in the question):And then it is quite simple to use:
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<<andoperator>>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.