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]

c++ template class problem



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.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
石井俊直   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


--------------------------------------------------------------
    // poi01.CC

    #include <iostream.h>
    
    template <class T> class C;
    
    template <class T> class C
    {
    public:
      T v;
      C() { v = 0; }
      ~C() {}
      friend ostream& operator << <>( ostream &, const C<T>& );
    };

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

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