| | |
passing template class objects as parameters to friend functions of the same class
![]() |
•
•
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 9:15 am. Reason: Added code tags, pointless, but I did it anyway.
•
•
Join Date: Dec 2006
Posts: 1,108
Reputation:
Solved Threads: 169
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:
Views: 621 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files filestream forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework http iamthwee input int lazy linker list loop loops map math matrix member memory multidimensional network newbie number object objects opengl output parameter pointer pointers problem program programming project qt random read reading recursion recursive reference server sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual visualstudio win32 window windows winsock






