Topic: ambiguity and templates


Author: rkdious@ix.netcom.com (Daniel)
Date: 1996/09/17
Raw View
#include <fstream>  // define class 'ofstream' and 'ostream'

//----------------------------------------------------
template< class T1 >
class TBase {
public:
   TBase( const T1 t ):itsA( t ){}
   ~TBase(){};
   friend ostream& operator<<( ostream& os, const TBase< T1 >& b );
protected:
   T1 itsA;
};

template< class T1 >
ostream& operator<<( ostream& os, const TBase< T1 >& b ){
   os << b.itsA;
   return os;
};

//----------------------------------------------------
template< class T2, class T3 >
class TAdvanced : public TBase< T2 > {
public:
   TAdvanced( const T2 t2, const T3 t3 ) : TBase< T2 >( t2 ), itsB( t3 ){};
   ~TAdvanced(){};
   friend ostream& operator<<( ostream& os, const TAdvanced< T2, T3 >& a );
protected:
   T3 itsB;
};

template< class T2, class T3 >
ostream& operator<<( ostream& os, const TAdvanced< T2, T3 >& a ){
   os << static_cast< TBase< T2 > >( a ) << itsB;
   return os;
};

//----------------------------------------------------
void main( int, char*[] ){
   ofstream ofs( "test.dat", ios::out | ios::binary );
   TAdvanced< int, float > test( 1, 2.3 );
   ofs << test;
};  // End main


In the above code my compiler complains about the line 'ofs << test;',
claiming that there is an ambiguity between
'operator<<( ostream&, const TBase< T1 >& )' and
'operator<<( ostream&, const TAdvanced< T2, T3 >& )'

Isn't it clear that 'test' is of type 'TAdvanced< int, float >' and not
of type 'TBase< int >' ?!!!!!!

Dazed and confused while using BC501.

-Daniel




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]