| | |
passing template class objects as parameters to friend functions of the same class
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 1
Reputation:
Solved Threads: 0
passing template class objects as parameters to friend functions of the same class
0
#1 Nov 18th, 2008
can u pass template class objects as parameters to friend functions of the same class??
i tried sumthin like...
i tried sumthin like...
C++ Syntax (Toggle Plain Text)
template<class T> class array { T a[10]; int n; public: friend istream& operator>>(istream&,array&); friend ostream& operator<<(ostream&,array&);}; istream& operator >>(istream& din,array& b) { din>>b.n ; //size of array for(inti=0;i<b.n;i++) din>>b.a[i]; return(din); } ostream& operator<<(ostream &dout,array &b) { for(int i=0;i<b.n;i++) dout<<b.a[i]<<" "; } void main() { array<int> iarray; cin>>iarray; cout<<iarray;}
Last edited by Narue; Nov 18th, 2008 at 10:15 am. Reason: Added code tags, pointless, but I did it anyway.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
Re: passing template class objects as parameters to friend functions of the same clas
0
#2 Nov 18th, 2008
> can u pass template class objects as parameters to friend functions of the same class??
obviously, you can. but you need to let the compiler know that they are templates.
obviously, you can. but you need to let the compiler know that they are templates.
c++ Syntax (Toggle Plain Text)
#include <iostream> // declare things first template< typename T > class array ; template< typename T > std::istream& operator>> ( std::istream& din, array<T>& b ) ; template< typename T > std::ostream& operator<< ( std::ostream &dout, const array<T> &b) ; // define them now template< typename T > class array { T a[10] ; // concept: T is default_constructible int n ; // <> tells the compiler that the friend is a template. // http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16 friend std::istream& operator>> <> ( std::istream&, array<T>& ) ; friend std::ostream& operator<< <> ( std::ostream&, const array<T>& ) ; //... }; template< typename T > std::istream& operator>> ( std::istream& din, array<T>& b ) { din >> b.n ; // what happens if the user enters a value > 10 ? for( int i=0; i<b.n; ++i ) din >> b.a[i] ; return din ; } template< typename T > std::ostream& operator<< ( std::ostream &dout, const array<T> &b) { dout << b.n << '\n' ; for( int i=0 ; i<b.n ; i++) dout<<b.a[i] << " " ; return dout << '\n' ; } int main() { array<int> iarray ; std::cin >> iarray ; std::cout << iarray ; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: USB handling using API in C++
- Next Thread: fatal error LNK1120:
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






