hey guys, i'm confused how access overloaded template function 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 function such as:
template <typename t> matrix(t r, t c) {rows=r; cols=c; elements=new t[r*c];} i able
matrix <double> test(number, number) for example, have no idea how use templated >> operator (or << or * or +..) appreciated. thanks!
i assuming declaring class template matrix has type argument t, , want use defined operator>> (you should make more explicit in question):
template <typename t> class matrix { int rows, cols; t* elements; public: matrix( int c, int r ); // want number of // rows/columns of type `t`?? // note: removed template, want befriend (and define) // single operator<< takes matrix<t> (the <t> optional // inside class braces friend std::istream& operator>>( std::istream& i, matrix& m ) { // m.rows, m.cols , m.elements accessible here. return i; } }; and quite simple use:
matrix<double> m( 1, 2 ); std::cin >> m; that not option, common one. in general class template can befriend single (non-templated) function (think operator<< , operator>> functions) in code above, or might want befriend function template (all instantiations) or particular instantiation of function template.
i wrote long explanation of different options , behaviors here, , recommend read it.
Comments
Post a Comment