From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alex Vinokur To: help-gcc@gnu.org Subject: Re: Template problem Date: Sat, 01 Apr 2000 00:00:00 -0000 Message-ID: <84neen$irc$1@nnrp1.deja.com> References: <38691EC2.598C836F@netvision.net.il> X-SW-Source: 2000-q1/msg00005.html Message-ID: <20000401000000.QY2ICJ8Pud-sqrG3gqJYygJOVaOKRv9W6y-gLA832WA@z> In article <38691EC2.598C836F@netvision.net.il>, Michael Ben-Gershon 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 Range > { > ... > }; > > It has a number of non-member functions defined for the various > operators. In particular, it has the following defined: > > template > ostream& > operator<< (ostream& out_stream, const Range& the_value); > > In the main program, I have > > int main() > { > Range 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 &' > /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 > class Floor_Number : public Range > { > ... > }; > > With the ostream stuff defined as: > > template > ostream& > operator<< (ostream& out_stream, const Floor_Number& > 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 //----------------------------------- template class Range { public : T value_; Range (const T& value_i) { value_ = value_i; } }; //----------------------------------- template ostream& operator<< (ostream& out_stream, const Range& the_value) { return out_stream << the_value.value_; } //----------------------------------- int main() { Range 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.