This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

template function compiling problem (alphaev5-linux)


Hi.

I have another problem about template. I divided the souce
into three parts, say poi02.H, poi02.C and main02.C

Then compilation fails like this.    

    g++ -g -O -frepo   -c poi02.C -o poi02.o
    g++ -g -O -frepo   -c main02.C -o main02.o
    g++ poi02.o main02.o
    collect: recompiling main02.C
    collect: relinking
    main02.o: In function `main':
    /ici/work/test_template/main02.C:9: undefined reference to \
	`ostream & operator<<<double>(ostream &, C<double> const &)'
    /ici/work/test_template/main02.C:9: undefined reference to \
	`ostream & operator<<<double>(ostream &, C<double> const &)'
    collect2: ld returned 1 exit status

The following compilation works.

    g++ -g -frepo -c poi02.C -o poi02.o
    g++ -g main02.C poi02.o

In fact, the following is enough.

    g++ main02.C

These results implies the template friend function are not
instantiated correctly although member function is done correctly.
In practical sense, it is not necessary to compile in the first
way. But to make egcs more sophisticated ...

The egcs is version 2.92.02 on alphaev5-linux.

Regards,

------------------------------------------------------------------------
    // poi02.H
    #include <iostream.h>
    template <class T> class C;
    template <class T> ostream& operator << ( ostream &, const C<T>& );

    template <class T> class C
    {
    public:
      T v;
      C() { v = 0; }
      ~C() {}
      C<T>& operator = (const T&);
    
      friend ostream& operator << <>( ostream&, const C<T>& );
    };
------------------------------------------------------------------------
    // poi02.C
    #include "poi02.H"

    template <class U>
    C<U>& C<U>::operator = ( const U& u )
    {
      v = u;
      return *this;
    }

    template <class U>
    ostream& operator << ( ostream &os, const C<U>& c )
    {
      os << c.v;
      return os;
    }
------------------------------------------------------------------------
    // main02.C
    #include "poi02.H"
    #include "poi02.C"
          
    int main()
    {
      C<double> tcd;
      tcd = 3.14;
      cout << tcd << endl;
    }


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
石井俊直   Toshinao Ishii

   Advanced Technology R&D Center (ATC)   三菱電機(株)
   Mitsubishi Electric Corporation        先端技術総合研究所

   email: ici@qua.crl.melco.co.jp (NeXTMail/MIME Welcome)
   fax:   +81-6-497-7288


P.S.

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

Will one tell me why this is better ? Faster in runtime ?

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]