public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: c++ template class problem
       [not found] <199809231033.TAA06844.cygnus.egcs@rothko.qua.srl.melco.co.jp>
@ 1998-09-24 19:20 ` Nathan Myers
  1998-09-25 22:53   ` Alexandre Oliva
  0 siblings, 1 reply; 8+ messages in thread
From: Nathan Myers @ 1998-09-24 19:20 UTC (permalink / raw)
  To: egcs

Toshinao ISHII wrote:

> I tried compiling the following C++ source using template.
> g++ (egcs-2.92.07) give the following error...
> --------------------------------------------------------------
>     // poi01.CC
> 
>     #include <iostream.h>
> 
>     template <class T> class C;

Add a forward declaration:
      template <class U>
        ostream& operator<<( ostream&, const C<U>& );
> 
>     template <class T> class C
>     {
>     public:
>       T v;
>       C() { v = 0; }
>       ~C() {}
>       friend ostream& operator << <>( ostream &, const C<T>& );

And the friend:

        template <class U>
          friend ostream& operator<<( ostream&, const C<U>& );
>     };
> 
>     template <class T>
>     ostream& operator << ( ostream &os, const C<T>& c )
>     {
>       os << c;
>       return os;
>     }
> 
>     int main()
>     {
>       C<double> tcd;
>       tcd.v = 3.14;
>       cout << tcd << endl;
>     }
> --------------------------------------------------------------

The rest is OK.  However, it's usually better to provide public
member function and then let the operator<< use the function:

>     template <class T> class C
>     {
>     public:
>       T v;
>       C() { v = 0; }
>       ~C() {}
>       void print( ostream& os) { os << c; }
      }
>     template <class T>
>       ostream& operator << ( ostream &os, const C<T>& c )
>     { c.print(os); return os; }

Nathan Myers
ncm@cygnus.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: c++ template class problem
  1998-09-24 19:20 ` c++ template class problem Nathan Myers
@ 1998-09-25 22:53   ` Alexandre Oliva
  0 siblings, 0 replies; 8+ messages in thread
From: Alexandre Oliva @ 1998-09-25 22:53 UTC (permalink / raw)
  To: Nathan Myers; +Cc: egcs

Nathan Myers <ncm@cygnus.com> writes:

> Toshinao ISHII wrote:

>>        friend ostream& operator << <>( ostream &, const C<T>& );

> And the friend:

>         template <class U>
>           friend ostream& operator<<( ostream&, const C<U>& );

I'm pretty sure you're well aware that these are not equivalent.

The former declaration refers to a single specialization, whereas the
latter refers to *all* specializations of the template function.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: c++ template class problem
  1998-09-25 20:49   ` Jason Merrill
@ 1998-09-25 22:53     ` Todd Vierling
  0 siblings, 0 replies; 8+ messages in thread
From: Todd Vierling @ 1998-09-25 22:53 UTC (permalink / raw)
  To: Jason Merrill; +Cc: egcs

On 25 Sep 1998, Jason Merrill wrote:

:  > : g++ -c poi01.C -g -frepo
:  > : g++ -o poi01 poi01.o
: 
:  > There are known but not admitted problems with -frepo on some platforms,
: 
: Not admitted?  That's an odd thing to say.

Well, it's been claimed that -frepo works, and I have yet to use it on a
platform on which it does.  My reports to that affect have gone unanswered.
The platforms include NetBSD, Solaris, SunOS 4.x, and BSD/I.

-- 
-- Todd Vierling (Personal tv@pobox.com; Bus. todd_vierling@xn.xerox.com)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: c++ template class problem
       [not found] ` <Pine.NEB.4.02.9809251448310.2414-100000.cygnus.egcs@duhnet.net>
@ 1998-09-25 20:49   ` Jason Merrill
  1998-09-25 22:53     ` Todd Vierling
  0 siblings, 1 reply; 8+ messages in thread
From: Jason Merrill @ 1998-09-25 20:49 UTC (permalink / raw)
  To: Todd Vierling, egcs

>>>>> Todd Vierling <tv@pobox.com> writes:

 > On Wed, 23 Sep 1998, Toshinao ISHII wrote:
 > : g++ -c poi01.C -g -frepo
 > : g++ -o poi01 poi01.o

 > There are known but not admitted problems with -frepo on some platforms,

Not admitted?  That's an odd thing to say.

Jason

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: c++ template class problem
  1998-09-23  8:43 Toshinao ISHII
  1998-09-24  2:37 ` Alexandre Oliva
@ 1998-09-25 15:38 ` Todd Vierling
       [not found] ` <Pine.NEB.4.02.9809251448310.2414-100000.cygnus.egcs@duhnet.net>
  2 siblings, 0 replies; 8+ messages in thread
From: Todd Vierling @ 1998-09-25 15:38 UTC (permalink / raw)
  To: Toshinao ISHII; +Cc: egcs

On Wed, 23 Sep 1998, Toshinao ISHII wrote:

: g++ -c poi01.C -g -frepo
: g++ -o poi01 poi01.o

There are known but not admitted problems with -frepo on some platforms,
but:  you do have a bug in your code unrelated to this.

:     template <class T>
:     ostream& operator << ( ostream &os, const C<T>& c )
:     {
:       os << c;
:       return os;
:     }

This function is recursive.  It will call itself because you are doing `os
<< c' - which will call `operator << (ostream&, const C<T>&)' again, ad
infinitum.  This needs to cast `c' to something which has a different
operator <<, or somehow extract the proper data, probably with:

        os << c.v;

-- 
-- Todd Vierling (Personal tv@pobox.com; Bus. todd_vierling@xn.xerox.com)


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: c++ template class problem
  1998-09-24  2:37 ` Alexandre Oliva
@ 1998-09-24  3:26   ` Toshinao Ishii
  0 siblings, 0 replies; 8+ messages in thread
From: Toshinao Ishii @ 1998-09-24  3:26 UTC (permalink / raw)
  To: egcs

Hi. 

: On which platform?  I can't reproduce this problem with the latest
: snapshot of egcs on sparc-sun-solaris2.5.

Thank you for reply. I forgot to write it. It is Linux/Alpha (alphaev5-linux).

: BTW, you must declare the template function before you refer to it, as
: suggested in the FAQ (the answer for friend templates was recently
: updated; please take a look at it).

I added one line of declaration as the following and tried. But, result
is same as before. Is the way of delclaration wrong ?

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
^[$B@P0f=SD>^[(B   Toshinao Ishii

   Advanced Technology R&D Center (ATC)   ^[$B;0I)EE5!^[(B(^[$B3t^[(B)
   Mitsubishi Electric Corporation        ^[$B@hC

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: c++ template class problem
  1998-09-23  8:43 Toshinao ISHII
@ 1998-09-24  2:37 ` Alexandre Oliva
  1998-09-24  3:26   ` Toshinao Ishii
  1998-09-25 15:38 ` Todd Vierling
       [not found] ` <Pine.NEB.4.02.9809251448310.2414-100000.cygnus.egcs@duhnet.net>
  2 siblings, 1 reply; 8+ messages in thread
From: Alexandre Oliva @ 1998-09-24  2:37 UTC (permalink / raw)
  To: Toshinao ISHII; +Cc: egcs

Toshinao ISHII <ici@qua.crl.melco.co.jp> writes:

> I tried compiling the following C++ source using template.

> g++ (egcs-2.92.07) give the following error.

> g++ -c poi01.C -g -frepo
> g++ -o poi01 poi01.o
> poi01.o: In function `main':
> /ici/work/test_template/poi01.C:27: undefined reference to \
>     `ostream & operator<<<double>(ostream &, C<double> const &)'

On which platform?  I can't reproduce this problem with the latest
snapshot of egcs on sparc-sun-solaris2.5.

BTW, you must declare the template function before you refer to it, as
suggested in the FAQ (the answer for friend templates was recently
updated; please take a look at it).

> In addition, the second parameter "C<double> const &" should be
> "const C<double> &".

Both are equivalent; the canonical representation prints cv-qualifiers
after the type to be qualified.

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


^ permalink raw reply	[flat|nested] 8+ messages in thread

* c++ template class problem
@ 1998-09-23  8:43 Toshinao ISHII
  1998-09-24  2:37 ` Alexandre Oliva
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Toshinao ISHII @ 1998-09-23  8:43 UTC (permalink / raw)
  To: egcs

Hi. I have just started using template in g++.

I tried compiling the following C++ source using template.
g++ (egcs-2.92.07) give the following error.

g++ -c poi01.C -g -frepo
g++ -o poi01 poi01.o
poi01.o: In function `main':
/ici/work/test_template/poi01.C:27: undefined reference to \
    `ostream & operator<<<double>(ostream &, C<double> const &)'
/ici/work/test_template/poi01.C:27: undefined reference to \
    `ostream & operator<<<double>(ostream &, C<double> const &)'
collect2: ld returned 1 exit status
gmake: *** [poi01] Error 1

It look g++ fail to instantiate the template function. In addition,
the second parameter "C<double> const &" should be
"const C<double> &". 

Please let me know how to implement the template function.

Thanks in advance.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
^[$B@P0f=SD>^[(B   Toshinao Ishii

   Advanced Technology R&D Center (ATC)   ^[$B;0I)EE5!^[(B(^[$B3t^[(B)
   Mitsubishi Electric Corporation        ^[$B@hC

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~1998-09-25 22:53 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <199809231033.TAA06844.cygnus.egcs@rothko.qua.srl.melco.co.jp>
1998-09-24 19:20 ` c++ template class problem Nathan Myers
1998-09-25 22:53   ` Alexandre Oliva
1998-09-23  8:43 Toshinao ISHII
1998-09-24  2:37 ` Alexandre Oliva
1998-09-24  3:26   ` Toshinao Ishii
1998-09-25 15:38 ` Todd Vierling
     [not found] ` <Pine.NEB.4.02.9809251448310.2414-100000.cygnus.egcs@duhnet.net>
1998-09-25 20:49   ` Jason Merrill
1998-09-25 22:53     ` Todd Vierling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).