public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alex Vinokur <alexander.vinokur@telrad.co.il>
To: help-gcc@gnu.org
Subject: Re: Template problem
Date: Sat, 01 Apr 2000 00:00:00 -0000	[thread overview]
Message-ID: <84neen$irc$1@nnrp1.deja.com> (raw)
Message-ID: <20000401000000.QY2ICJ8Pud-sqrG3gqJYygJOVaOKRv9W6y-gLA832WA@z> (raw)
In-Reply-To: <38691EC2.598C836F@netvision.net.il>

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.

  parent reply	other threads:[~2000-04-01  0:00 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-28 13:11 Michael Ben-Gershon
1999-12-31 22:24 ` Michael Ben-Gershon
2000-01-02  4:22 ` Alex Vinokur [this message]
2000-04-01  0:00   ` Alex Vinokur
2001-04-02  7:20 jpyubero
2001-04-02 21:00 ` Alexandre Oliva
2002-10-09  9:33 template problem andy
2002-10-09 10:18 ` Eljay Love-Jensen
     [not found] <2F05A390F72A0A409390E016D23E45E8042DBE10@ns-bco-mse4.im.battelle.org>
2002-10-09 11:46 ` andy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='84neen$irc$1@nnrp1.deja.com' \
    --to=alexander.vinokur@telrad.co.il \
    --cc=help-gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).