This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Template problem
- To: help-gcc at gnu dot org
- Subject: Re: Template problem
- From: Alex Vinokur <alexander dot vinokur at telrad dot co dot il>
- Date: Sun, 02 Jan 2000 12:12:19 GMT
- Newsgroups: gnu.gcc.help,gnu.g++.help
- Organization: Deja.com - Before you buy.
- References: <38691EC2.598C836F@netvision.net.il>
- Xref: wodc7nx0 gnu.gcc.help:2327 gnu.g++.help:1274
In article <38691EC2.598C836F@netvision.net.il>,
Michael Ben-Gershon <mybg@netvision.net.il> wrote:
> 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
>
The following code has no problem when
compiling on g++ (egcs-2.91.5).
Alex
//#########################################################
//------------------- C++ code : BEGIN -------------------
#include <iostream>
//-----------------------------------
template <class T, T lower, T upper>
class Range
{
public :
T value_;
Range (const T& value_i)
{
value_ = value_i;
}
};
//-----------------------------------
template<class T, T lower, T upper>
ostream&
operator<< (ostream& out_stream, const Range<T, lower, upper>&
the_value)
{
return out_stream << the_value.value_;
}
//-----------------------------------
int main()
{
Range<int, -3, 5> f (-2);
cout << endl;
cout << "Number is: " << f << endl;
return 0;
}
//------------------- C++ code : END ----------------------
//#########################################################
//------------------- Running Results : BEGIN -------------
Number is: -2
//------------------- Running Results : END ---------------
//#########################################################
//------------------- Environment -------------------------
g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)
uname -sr : SunOS 5.6
//---------------------------------------------------------
//#########################################################
Sent via Deja.com http://www.deja.com/
Before you buy.