Advertisement

Overloading << and >> (friends not very friendly)

Started by July 23, 2002 12:44 PM
4 comments, last by Anomaly_E 22 years, 1 month ago
Ok, this probably is a stupid error but I still can''t see what I am doing wrong: I''m overloading the << and the >> and the compiler (VC++6) complains over that I''m trying to access private member variables in operator<<() and operator>>(), even though I have declared them as friends. Here is the code: /*LObject.h****************************************************/ ... class LObject{ friend ostream& operator<<(ostream&, const LObject&); friend istream& operator>>(istream&, LObject&); ... private: short m_type; int m_object; }; ostream& operator<<(ostream&, const LObject&); istream& operator>>(istream&, LObject&); /*LObject.cpp**************************************************/ ... ostream& operator <<(ostream &os, const LObject &lo){ /*Compile error here, can''t access private members m_type and m_object.*/ os << lo.m_type << lo.m_object; return os; } istream& operator >>(istream &is, LObject &lo){ /*Compile error here, can''t access private members m_type and m_object.*/ is >> lo.m_type; is >> lo.m_object; return is; } /*End of Code**************************************************/ Anyone see what I''m doing wrong? Shouldn''t it work to access private members in functions that are friends to the class? Regards Bjorn
Your istream/ostream isn't the one accessing the variables, it's your current namespace...

Just remove those friend declaretions, they're useless...

3 possible soloutions:

1: I don't know if you can friend a namespace, try doing so..
2: Make them public..
3: you could put that code inside a class which you declare as a friend...

[edited by - Coward on July 23, 2002 2:48:43 PM]
Don't Temp Fate..
Advertisement
Hi Borjn

I remember when I taught C++ last semester, when we got to operator overloading all hell broke loose because everyone was getting this error.

It's a bug in visual C++ pre-Service Pack 3, basically
it IGNORES all friend declarations after a using namespace std.
A more detailed explanation can be found here
http://www.cs.virginia.edu/~cs201/Help/mistakes.html

And from Microsoft
http://support.microsoft.com/default.aspx?scid=KB;EN-US;q192539&

Coward,
You can not declare insertion or extraction operators as a member function public or otherwise, you must either use friend functions or add accessor methods to your class. But friend functions are the prefered method from what I have seen.

Good Luck,
- James

[edited by - James555 on July 23, 2002 2:59:47 PM]
quote: Original post by Coward
Your istream/ostream isn''t the one accessing the variables, it''s your current namespace...

Just remove those friend declaretions, they''re useless...

3 possible soloutions:

1: I don''t know if you can friend a namespace, try doing so..
2: Make them public..
3: you could put that code inside a class which you declare as a friend...

[edited by - Coward on July 23, 2002 2:48:43 PM]


the "friend ostream & operator..." and "friend istream & operator..." declare functions as friends, not classes. ostream and istream are return types.


Don''t listen to me. I''ve had too much coffee.
Ah, thanks! I installed the latest service pack and now it works...
That''s what you get when you don''t read the OP...

I just looked at the friend declerations and the place where he accessed the members, then I figured "he thinks that making the << op friend of class, he can access private members using the ops"
I never actualy got to the point reading that he was actualy declaring the ops, not just using them..
Realized this little later thinking of the code, but at that time my comp was shut down and figured what the heck, someone will correct me...

Sorry about that...
Don't Temp Fate..

This topic is closed to new replies.

Advertisement