[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

How to define inserter or extractor for a class template

laikon

10/8/2008 6:57:00 AM

this question is about how to define friend functions for a class
template.
the following is an example.

template <typename T>
class Array
{
private:
T* parr;
int sz;

friend ostream& operator << (ostream& os, const Array<T>& rhs);
};

template <class T>
ostream& operator << (ostream& os, const Array<T>& rhs)
{
for (int i = 0; i < rhs.sz; ++i)
os << rhs.parr[i] << "\t";
return os;
}

these codes work well in VC++ 6.0, while linking errors are given in VC
++ 2005.
3 Answers

Barry

10/8/2008 7:20:00 AM

0

On Oct 8, 2:57 pm, laikon <lai...@gmail.com> wrote:
> this question is about how to define friend functions for a class
> template.
> the following is an example.
>
> template <typename T>
> class Array
> {
> private:
>         T* parr;
>         int sz;
>
>         friend ostream& operator << (ostream& os, const Array<T>& rhs);

friend ostream& operator<< <T> (ostream& os, const Array<T>& rhs);

>
> };
>
> template <class T>
> ostream& operator << (ostream& os, const Array<T>& rhs)
> {
>         for (int i = 0; i < rhs.sz; ++i)
>                 os << rhs.parr[i] << "\t";
>         return os;
>
> }
>
> these codes work well in VC++ 6.0, while linking errors are given in VC
> ++ 2005.

--
Best Regards
Barry

Barry

10/8/2008 7:50:00 AM

0

On Oct 8, 3:19 pm, Barry <dhb2...@gmail.com> wrote:
> On Oct 8, 2:57 pm, laikon <lai...@gmail.com> wrote:
>
> > this question is about how to define friend functions for a class
> > template.
> > the following is an example.
>
> > template <typename T>
> > class Array
> > {
> > private:
> >         T* parr;
> >         int sz;
>
> >         friend ostream& operator << (ostream& os, const Array<T>& rhs);
>
> friend ostream& operator<< <T> (ostream& os, const Array<T>& rhs);
>

Note that this is only a work around for VC2005.
According to 10.5.3
We should forward declaring "template operator<<", which requires
forward
declaration of "template class Array".
So the code looks like this:

template <typename T>
class Array;

template <typename T>
ostream& operator<< (ostream&, Array<T> const&);

template <typename T>
class Array {
...

friend
ostream& operator<< <T> (ostream&, Array const&); // Array<T> is
also fine
};

template <typename T>
ostream& operator<< (ostream& ostrm, Array<T> const& a) {
...
}

>
>
> > };
>
> > template <class T>
> > ostream& operator << (ostream& os, const Array<T>& rhs)
> > {
> >         for (int i = 0; i < rhs.sz; ++i)
> >                 os << rhs.parr[i] << "\t";
> >         return os;
>
> > }
>
> > these codes work well in VC++ 6.0, while linking errors are given in VC
> > ++ 2005.
>

--
Best Regards
Barry

James Kanze

10/8/2008 9:38:00 AM

0

On Oct 8, 8:57 am, laikon <lai...@gmail.com> wrote:
> this question is about how to define friend functions for a
> class template.

It's difficult:-).

> the following is an example.

> template <typename T>
> class Array
> {
> private:
> T* parr;
> int sz;

> friend ostream& operator << (ostream& os, const Array<T>& rhs);

Note that this declares a non-template operator<< as a friend.
That's probably not what you want.

> };

> template <class T>
> ostream& operator << (ostream& os, const Array<T>& rhs)
> {
> for (int i = 0; i < rhs.sz; ++i)
> os << rhs.parr[i] << "\t";
> return os;
> }

> these codes work well in VC++ 6.0, while linking errors are
> given in VC ++ 2005.

If you have a declaration of the operator<<, as a template,
before you define the class, I think it should work. In
practice, I've found it more convenient to only use inline
friends, so the problem doesn't come up. Something like:

template< typename T >
class Array
{
// ...
public:
void print( std::ostream& dest ) const ;
friend std::ostream&
operator<<( std::ostream& dest,
Array< T > const& array )
{
array.print( dest ) ;
return *this ;
}
} ;

The friend may be a non-template function, but it doesn't
matter, since I don't have to declare it anywhere else.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34