Advertisement

simple operator << problem in VC++6.0

Started by July 20, 2000 02:01 PM
1 comment, last by Tuaron 24 years, 5 months ago
Hi everyone! I need some help here. I''m learning C++ and use VC++ 6.0 and in the book there is an example. Here is the header file (strng1.h). #ifndef _STRNG1_H_ #define _STRNG1_H_ #include using namespace std; class String { private: char *str; int len public: //some functions... friend ostream &operator <<(ostream &os, const String &st); }; #endif And here is strng1.cpp #include #include "strng1.h" using namespace std; //Some functions... ostream &operator <<(ostream &os, const String &st) { os << st.str; return os; } Now, VC++ gives this error message... Compiling... strng1.cpp d:\devstudio\myprojects\listning 11_1\strng1.cpp(36) : error C2248: ''str'' : cannot access private member declared in class ''String'' d:\devstudio\myprojects\listning 11_1\strng1.h(9) : see declaration of ''str'' vegnyhet.cpp Can you tell me what is wrong? I have seen the same example in another book so I think there is something wrong with VC++. Please tell me how to make this work. Thanx!
A friend should be able to access private members. I know what the problem is, you are right, VC is to blame!

Before anyone slags me off, I have looked into this! Service Pack 4 for visual studio fixes this problem which only occurs with the << operator.

Hope this helps! (the service pack is a big d/l though)
Advertisement
Thanx!

This topic is closed to new replies.

Advertisement