parker@grapevine.uucp (Parker Waechter) (07/25/89)
The following piece of code generates an error, and I can't see why
#include <stream.h>
class complex {
double r;
double i;
public:
complex() {r = 0;i = 0;}
complex(double rv, double iv = 0) {r = rv; i = iv;}
friend ostream& operator << (ostream& s, complex& comp);
};
ostream& complex::operator <<(ostream& s, complex& comp)
{s << comp.r << " " << comp.i << "i" ;
return (s);
}
The error it generates is:
"comp2.C", line 14: error: operator <<() is not a member of complex
why not?
----------------------
parker@sun.com.uucp
These are my questions/opinions/problems - not Sun'sark@alice.UUCP (Andrew Koenig) (07/25/89)
In article <33951@grapevine.uucp>, parker@grapevine.uucp (Parker Waechter) writes: > The error it generates is: > "comp2.C", line 14: error: operator <<() is not a member of complex > > why not? Because friends aren't members. If you say class Foo { friend void bar(); }; then when you define bar(), you merely say void bar() { /* stuff */ } and not void Foo::bar() { /* stuff */ } -- --Andrew Koenig ark@europa.att.com
dog@cbnewsl.ATT.COM (edward.n.schiebel) (07/25/89)
From article <33951@grapevine.uucp>, by parker@grapevine.uucp (Parker Waechter): > class complex { >... > friend ostream& operator << (ostream& s, complex& comp); > }; > > ostream& complex::operator <<(ostream& s, complex& comp) {...} > "comp2.C", line 14: error: operator <<() is not a member of complex > why not? Friend functions are not members, just plain old functions which happen to enjoy access to private date of the class(es) they are friends of. Your class declaration specifies a function "ostream& operator<<" and not "ostream& complex::operator<<". Ed Schiebel AT&T Bell Laboratories
dan@oresoft.uu.net (Daniel Elbaum) (07/26/89)
In article <33951@grapevine.uucp> parker@sun.UUCP (Parker Waechter) writes:
:The following piece of code generates an error, and I can't see why
...
:class complex {
...
: friend ostream& operator << (ostream& s, complex& comp);
:};
:
:ostream& complex::operator <<(ostream& s, complex& comp)
:{
...
:}
:
:The error it generates is:
:"comp2.C", line 14: error: operator <<() is not a member of complex
:
:why not?
:----------------------
:parker@sun.com.uucp
:These are my questions/opinions/problems - not Sun's
A friend is not a member and so neither requires nor allows the member
access syntax *::*. See section 8.5.10 of the C++ reference manual
in The C++ Programming Language or sec. 11.4 of subsequent draft
versions of the of the new reference manual.
--
The workaday world must remain transparent to those who maintain it if
they are to find inspired within them a vision of the history they create.
({uunet,tektronix,reed,sun!nosun,osu-cis,psu-cs}!oresoft!(dan)@oresoft.uu.net)