template function compiling problem (alphaev5-linux)
Toshinao ISHII
ici@qua.crl.melco.co.jp
Fri Sep 25 06:49:00 GMT 1998
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;
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
$B@P0f=SD>(B Toshinao Ishii
Advanced Technology R&D Center (ATC) $B;0I)EE5!(B($B3t(B)
Mitsubishi Electric Corporation $B@hC
More information about the Gcc
mailing list