This is the mail archive of the gcc-help@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 problem


I am having problems with templates on gcc-2.95.2. I am sure
that the code worked on some earlier version of gcc, but I
don't recall exactly which version it was.

I have a Range class defined as:

template <class T, T lower, T upper>
class Range
{
...
};

It has a number of non-member functions defined for the various
operators. In particular, it has the following defined:

template<class T, T lower, T upper>
ostream&
operator<< (ostream& out_stream, const Range<T, lower, upper>& the_value);

In the main program, I have

int main()
{
  Range<int, -5, 5> f = -5;

  cout << endl;
  
  cout << "Number is: " << f << endl;
...

When compiling (all the code, including the implementation of the 
function above, is in one file) I get the following error:

test_range.cc: In function `int main()':
test_range.cc:18: no match for `ostream & << Range<int,-5,5> &'
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/iostream.h:77:
candidates are: class ostream & ostream::operator <<(char)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/iostream.h:78:                
class ostream & ostream::operator <<(unsigned char)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.2/../../../../include/g++-3/iostream.h:79:                
class ostream & ostream::operator 

etc, etc, etc ...

The strange thing is that I have a derived class, which behaves fine!
It is defined as:

template<int lowest, int highest>
class Floor_Number : public Range<int, lowest, highest>
{
...
};

With the ostream stuff defined as:

template<int lowest, int highest>
ostream&
operator<< (ostream& out_stream, const Floor_Number<lowest, highest>&
the_value);

and is used in the test program as:

Floor_Number<-3, 10> Ff = -5;

cout << "The floor is: " << Ff << endl;

What is wrong with the Range stuff? If it is wrong, how is it that the derived
class Floor_Number is OK?

Thanks,

Michael Ben-Gershon
mybg@netvision.net.il


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