passing template class objects as parameters to friend functions of the same class

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 1
Reputation: spikeglow is an unknown quantity at this point 
Solved Threads: 0
spikeglow spikeglow is offline Offline
Newbie Poster

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...
  1. template<class T>
  2. class array
  3. {
  4. T a[10];
  5. int n;
  6. public:
  7. friend istream& operator>>(istream&,array&);
  8. friend ostream& operator<<(ostream&,array&);};
  9.  
  10. istream& operator >>(istream& din,array& b)
  11. {
  12. din>>b.n ; //size of array
  13. for(inti=0;i<b.n;i++)
  14. din>>b.a[i];
  15. return(din);
  16. }
  17.  
  18. ostream& operator<<(ostream &dout,array &b)
  19. {
  20. for(int i=0;i<b.n;i++)
  21. dout<<b.a[i]<<" ";
  22. }
  23. void main()
  24. {
  25. array<int> iarray;
  26. cin>>iarray;
  27. cout<<iarray;}
Last edited by Narue; Nov 18th, 2008 at 10:15 am. Reason: Added code tags, pointless, but I did it anyway.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

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.
  1. #include <iostream>
  2.  
  3. // declare things first
  4. template< typename T > class array ;
  5.  
  6. template< typename T >
  7. std::istream& operator>> ( std::istream& din, array<T>& b ) ;
  8.  
  9. template< typename T >
  10. std::ostream& operator<< ( std::ostream &dout, const array<T> &b) ;
  11.  
  12. // define them now
  13. template< typename T > class array
  14. {
  15. T a[10] ; // concept: T is default_constructible
  16. int n ;
  17.  
  18. // <> tells the compiler that the friend is a template.
  19. // http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16
  20. friend std::istream& operator>> <> ( std::istream&, array<T>& ) ;
  21. friend std::ostream& operator<< <> ( std::ostream&,
  22. const array<T>& ) ;
  23. //...
  24. };
  25.  
  26. template< typename T >
  27. std::istream& operator>> ( std::istream& din, array<T>& b )
  28. {
  29. din >> b.n ; // what happens if the user enters a value > 10 ?
  30. for( int i=0; i<b.n; ++i ) din >> b.a[i] ;
  31. return din ;
  32. }
  33.  
  34. template< typename T >
  35. std::ostream& operator<< ( std::ostream &dout, const array<T> &b)
  36. {
  37. dout << b.n << '\n' ;
  38. for( int i=0 ; i<b.n ; i++) dout<<b.a[i] << " " ;
  39. return dout << '\n' ;
  40. }
  41.  
  42. int main()
  43. {
  44. array<int> iarray ;
  45. std::cin >> iarray ;
  46. std::cout << iarray ;
  47. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC