473,326 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

template class objects as parameters

can u pass template class objects as parameters to friend functions of the same class??
i tried sumthin like...
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;}


please help!!
Nov 9 '06 #1
1 1591
Obviously, you can. but you need to let the compiler know that they are templates.




C++ Syntax

#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 ;

}
Nov 19 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Istvan Buki | last post by:
Dear C++ gurus, I'm having some problems inserting some template classes into containers. I'm looking for ideas, suggestions,... on how to achieve this but first let me explain my problem....
2
by: CoolPint | last post by:
Can anyone kindly explain why non-type template parameters are required by giving some examples where their uses are clearly favourable to other alternatives? I cannot think of any good use for...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
by: Nike | last post by:
I have a small question w.r.t usage of default arguements in template.. I shall try to elaborate this with an example.. let's say I have some template function , where EntryType is the input for...
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.