| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"fabio de francesco" |
| Date: |
22 Jun 2004 08:46:44 AM |
| Object: |
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
.
|
|
| User: "Christian Janßen" |
|
| Title: Re: access of protected function members from friends |
22 Jun 2004 10:24:36 AM |
|
|
[snip]
friend ostream& operator<<( ostream &, Person & );
friend istream& operator>>( istream &, Person & );
Refs here..
inline ostream& operator<<( ostream &out, Person prs )
{
return prs.writeToConsole( out );
}
inline istream& operator>>( istream &in, Person prs )
{
return prs.readFromConsole( in );
}
Objs here...
=> not friends.
.
|
|
|
| User: "Howard" |
|
| Title: Re: access of protected function members from friends |
22 Jun 2004 02:23:03 PM |
|
|
"Christian Janßen" <cj@christian-janszen.de> wrote in message
news:40d84f37_1@news.arcor-ip.de...
[snip]
friend ostream& operator<<( ostream &, Person & );
friend istream& operator>>( istream &, Person & );
Refs here..
inline ostream& operator<<( ostream &out, Person prs )
{
return prs.writeToConsole( out );
}
inline istream& operator>>( istream &in, Person prs )
{
return prs.readFromConsole( in );
}
Objs here...
=> not friends.
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
.
|
|
|
| User: "fabio de francesco" |
|
| Title: Re: access of protected function members from friends |
23 Jun 2004 12:25:23 PM |
|
|
"Howard" <alicebt@hotmail.com> wrote in message news:<rG%Bc.116282$Gx4.29709@bgtnsc04-news.ops.worldnet.att.net>...
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.
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
.
|
|
|
|
|
|

|
Related Articles |
|
|