943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1238
  • C++ RSS
Nov 18th, 2008
0

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

Expand Post »
can u pass template class objects as parameters to friend functions of the same class??
i tried sumthin like...
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spikeglow is offline Offline
1 posts
since Nov 2008
Nov 18th, 2008
0

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

> 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.
c++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: USB handling using API in C++
Next Thread in C++ Forum Timeline: fatal error LNK1120:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC