public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* friend operators
@ 2002-08-15 10:06 Bernd Speiser
  2002-08-26 10:47 ` Gokhan Kisacikoglu
  0 siblings, 1 reply; 3+ messages in thread
From: Bernd Speiser @ 2002-08-15 10:06 UTC (permalink / raw)
  To: gcc-help

Hi,
I´m still using gcc 2.95.3 on a SuSE Linux 7.3 system.

I´m trying to write some C++ code which uses operator overloading in a
friend situation. Also this is within a template (but I have already
observed all requirements pointed to in the FAQ, so this is ok, and the
problem also appears in non-template classes).

When I have a declaration like

friend ostream & operator<< (ostream &, VariableQuantity<U, D> &);

and I declare later another operator<< with different argument types,
e.g.

void operator<< (string &);

this does not work if the second operator<< is NOT a friend.
Then I get the error message

../Quantity/Quantity.h: In instantiation of `Quantities::VariableQuantity<Units:
:QuantityUnit<Quantities::LengthUnitClass>,Dimensions::QuantityDimension<Quantit
ies::LengthDimensionClass> >':
Length.h:431:   instantiated from here
../Quantity/Quantity.h:852: invalid use of undefined type `class Quantities::Var
iableQuantity<Units::QuantityUnit<Quantities::LengthUnitClass>,Dimensions::Quant
ityDimension<Quantities::LengthDimensionClass> >'
../Quantity/Quantity.h:907: forward declaration of `class Quantities::VariableQu
antity<Units::QuantityUnit<Quantities::LengthUnitClass>,Dimensions::QuantityDime
nsion<Quantities::LengthDimensionClass> >'
../Quantity/Quantity.h:852: confused by earlier errors, bailing out

when I try to instantiate the template (or, if this is not a template class, if
I just want to use the class).
If the second operator<< is also a friend, it compiles ok.

Is there something illegal about my code?

Thanks for any help.
Bernd
-- 
=======================================================================
Bernd Speiser
Institut f"ur Organische Chemie
Auf der Morgenstelle 18
D-72076 T"ubingen
Germany
phone: +49-7071-2976205 (office) +49-7071-2976242 (laboratory)
fax:   +49-7071-295518 (new!)
e-mail: bernd.speiser@uni-tuebingen.de
Internet: http://www.uni-tuebingen.de/speiser
=======================================================================

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: friend operators
  2002-08-15 10:06 friend operators Bernd Speiser
@ 2002-08-26 10:47 ` Gokhan Kisacikoglu
  2002-08-27  6:43   ` Bernd Speiser
  0 siblings, 1 reply; 3+ messages in thread
From: Gokhan Kisacikoglu @ 2002-08-26 10:47 UTC (permalink / raw)
  To: bernd.speiser; +Cc: gcc-help

> void operator<< (string &);


if you encapsulate it within a namespace, don't, it doesn't seem to
work, you should declare this operator globally for it to work, I've
seen this work with other compilers, but not with gcc...

Gokhan


> 
> this does not work if the second operator<< is NOT a friend.
> Then I get the error message
> 
> ../Quantity/Quantity.h: In instantiation of `Quantities::VariableQuantity<Units:
> :QuantityUnit<Quantities::LengthUnitClass>,Dimensions::QuantityDimension<Quantit
> ies::LengthDimensionClass> >':
> Length.h:431:   instantiated from here
> ../Quantity/Quantity.h:852: invalid use of undefined type `class Quantities::Var
> iableQuantity<Units::QuantityUnit<Quantities::LengthUnitClass>,Dimensions::Quant
> ityDimension<Quantities::LengthDimensionClass> >'
> ../Quantity/Quantity.h:907: forward declaration of `class Quantities::VariableQu
> antity<Units::QuantityUnit<Quantities::LengthUnitClass>,Dimensions::QuantityDime
> nsion<Quantities::LengthDimensionClass> >'
> ../Quantity/Quantity.h:852: confused by earlier errors, bailing out
> 
> when I try to instantiate the template (or, if this is not a template class, if
> I just want to use the class).
> If the second operator<< is also a friend, it compiles ok.
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: friend operators
  2002-08-26 10:47 ` Gokhan Kisacikoglu
@ 2002-08-27  6:43   ` Bernd Speiser
  0 siblings, 0 replies; 3+ messages in thread
From: Bernd Speiser @ 2002-08-27  6:43 UTC (permalink / raw)
  To: kisa; +Cc: gcc-help

Gokhan Kisacikoglu wrote:
> 
> > void operator<< (string &);
> 
> if you encapsulate it within a namespace, don't, it doesn't seem to
> work, you should declare this operator globally for it to work, I've
> seen this work with other compilers, but not with gcc...
> 
 
Thanks for this information. However, I need the namespace in this case.

I've kind of solved the problem in the meantime (following a suggestion
mentioned
in the gcc-bugs mailinglist:
I declare ALL operator<< functions in this class to be friends. Of course, this
means that those taking the class itself as a first argument, I have to give the
class explicitly
such as

friend void operator<< (Quantity &, string &);
friend istream & operator<< (Quantity &, istream &);

so I can write

Quantity Q;

Q << "something";

istream is;

Q << is;


etc.

Bernd

>

-- 
=======================================================================
Bernd Speiser
Institut f"ur Organische Chemie
Auf der Morgenstelle 18
D-72076 T"ubingen
Germany
phone: +49-7071-2976205 (office) +49-7071-2976242 (laboratory)
fax:   +49-7071-295518
e-mail: bernd.speiser@uni-tuebingen.de
Internet: http://www.uni-tuebingen.de/speiser
=======================================================================

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-08-27 13:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-15 10:06 friend operators Bernd Speiser
2002-08-26 10:47 ` Gokhan Kisacikoglu
2002-08-27  6:43   ` Bernd Speiser

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).