
July 22nd, 2005, 02:35 PM
| | | access of protected function members from friends
Hello,
I have written this code that compiles without errors ( "..." stays
for code that I don't post for the sake of brevity):
// Person.h
....
class Person
{
public:
Person();
...
protected:
...
ostream& writeToConsole( ostream & ) {...};
istream& readFromConsole( istream & ) {...};
friend ostream& operator<<( ostream &out, Person &prs )
{ return prs.writeToConsole(out); }
friend istream& operator>>( istream &in, Person &prs )
{ return prs.readFromConsole(in); }
};
....
If I try to extract the code from the friend functions and put it in a
different compilation unit, like this:
// Person.h
....
class Person
{
public:
Person();
...
protected:
...
ostream& writeToConsole( ostream & ) {...};
istream& readFromConsole( istream & ) {...};
friend ostream& operator<<( ostream &, Person & );
friend istream& operator>>( istream &, Person & );
};
....
// Person.cpp
....
inline ostream& operator<<( ostream &out, Person prs )
{
return prs.writeToConsole( out );
}
inline istream& operator>>( istream &in, Person prs )
{
return prs.readFromConsole( in );
}
....
I got the following from the compiler:
/sources/C++_studio/datastructures/database/src/person.h: In function
`
*std::ostream& operator<<(std::ostream&, Person)':
*/sources/C++_studio/datastructures/database/src/person.h:39: error: `
std::ostream& Person::writeToConsole(std::ostream&)' is protected
*/sources/C++_studio/datastructures/database/src/person.cpp:24: error:
within this context
*/sources/C++_studio/datastructures/database/src/person.h: In function
`
*std::istream& operator>>(std::istream&, Person)':
*/sources/C++_studio/datastructures/database/src/person.h:40: error: `
std::istream& Person::readFromConsole(std::istream&)' is protected
*/sources/C++_studio/datastructures/database/src/person.cpp:29: error:
within this context
*gmake[2]: *** [person.o] Error 1
Please would someone explain what I am missing? In particular I would
like to know why these errors come out only if I put the friends
implementation on a different compilation unit.
Thank you in advance for every help.
Ciao,
Fabio De Francesco | 
July 22nd, 2005, 02:36 PM
| | | Re: access of protected function members from friends
[snip][color=blue]
> friend ostream& operator<<( ostream &, Person & );
> friend istream& operator>>( istream &, Person & );[/color]
Refs here..
[color=blue]
> inline ostream& operator<<( ostream &out, Person prs )
> {
> return prs.writeToConsole( out );
> }
>
> inline istream& operator>>( istream &in, Person prs )
> {
> return prs.readFromConsole( in );
> }[/color]
Objs here...
=> not friends. | 
July 22nd, 2005, 03:05 PM
| | | Re: access of protected function members from friends
"Christian Janßen" <cj@christian-janszen.de> wrote in message
news:40d84f37_1@news.arcor-ip.de...[color=blue]
> [snip][color=green]
> > friend ostream& operator<<( ostream &, Person & );
> > friend istream& operator>>( istream &, Person & );[/color]
> Refs here..
>[color=green]
> > inline ostream& operator<<( ostream &out, Person prs )
> > {
> > return prs.writeToConsole( out );
> > }
> >
> > inline istream& operator>>( istream &in, Person prs )
> > {
> > return prs.readFromConsole( in );
> > }[/color]
>
> Objs here...
> => not friends.
>
>[/color]
In case that wasn't clear, what he means is that your Person parameters are
defined as reference parameters in the friend declaratons, but as by-value
parameters in the function definitions. Therefore, they are not the friends
described in the class, but actually completely different functions.
-Howard | 
July 22nd, 2005, 04:16 PM
| | | Re: access of protected function members from friends
"Howard" <alicebt@hotmail.com> wrote in message news:<rG%Bc.116282$Gx4.29709@bgtnsc04-news.ops.worldnet.att.net>...[color=blue]
> In case that wasn't clear, what he means is that your Person parameters are
> defined as reference parameters in the friend declaratons, but as by-value
> parameters in the function definitions. Therefore, they are not the friends
> described in the class, but actually completely different functions.[/color]
I'm sorry, what a silly oversight!
I didn't check the correspondance between parameters becouse I was
sure to have copied the functions as they were in the class definition
and furthermore I was misguided from the resulting errors.
I always rely on the compiler that says it cannot find the candidate
in class definition when I don't use the parameters as they were
declared in the class; but this time they were friend functions and
so ...
One more reason to limit the use of "friends", I suppose.
Thank you,
Fabio De Francesco |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|