access of protected function members from friends

F

fabio de francesco

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::eek:stream& operator<<(std::eek:stream&, Person)':
*/sources/C++_studio/datastructures/database/src/person.h:39: error: `
std::eek:stream& Person::writeToConsole(std::eek:stream&)' 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
 
C

Christian Janßen

[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.
 
H

Howard

Christian Janßen said:
[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
 
F

fabio de francesco

Howard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top