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