This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
template bug
- To: gcc-bugs at gcc dot gnu dot org
- Subject: template bug
- From: JM Geremia <jgeremia at princeton dot edu>
- Date: Thu, 10 Feb 2000 11:25:05 -0500
The preprocessed code below generates the following compiler error:
foo.C: In instantiation of `foo<double>':
foo.C:45: instantiated from here
foo.C:16: invalid use of undefined type `class foo<double>'
foo.C:23: forward declaration of `class foo<double>'
Here are the vitals:
g++ -v
Reading specs from /usr/local/lib/gcc-lib/mips-sgi-irix6.5/2.95.2/specs
gcc version 2.95.2 19991024 (release)
uname
IRIX mozart 6.5 10181059 IP32 mips
BUG DESCRIPTION:
This is an example program written to demonstrate the bug. The problem
occurs when operator*() is declared as both a friend and member. No
problem occurs when operator*() is declared only as a friend or only as a
member. Comment out the member definition and declaration of operator*()
and everything works.
Thanks for any help.
JM
P.S. please cc me. I'm not on the list.
---------- BEGIN SOURCE ----------
# 1 "foo.C"
template <class EType>
class foo;
template <class EType>
foo<EType> operator*( const EType &, const foo<EType> & );
template <class EType>
class foo {
public:
foo( const EType v = 0 ) : Value( v ) {};
virtual ~foo( ) {};
friend foo<EType> operator* <> ( const EType &, const foo<EType> & );
foo<EType> operator*( const EType & );
private:
EType Value;
};
template <class EType>
foo<EType> operator*( const EType & e, const foo<EType> & f )
{
return foo<EType>( e * f.Value );
}
template <class EType>
foo<EType> foo<EType>::operator*( const EType & e )
{
return foo<EType>( e * Value );
}
int main( )
{
foo<double> f( 5.0 );
return 0;
}
template <class EType>
class foo;
template <class EType>
foo<EType> operator*( const EType &, const foo<EType> & );
template <class EType>
class foo {
public:
foo( const EType v = 0 ) : Value( v ) {};
virtual ~foo( ) {};
friend foo<EType> operator* <> ( const EType &, const foo<EType> & );
foo<EType> operator*( const EType & );
private:
EType Value;
};
// Friend Function operator* definition
template <class EType>
foo<EType> operator*( const EType & e, const foo<EType> & f )
{
return foo<EType>( e * f.Value );
}
// Member Function operator * definition
template <class EType>
foo<EType> foo<EType>::operator*( const EType & e )
{
return foo<EType>( e * Value );
}
int main( )
{
foo<double> f( 5.0 );
return 0;
}