C++ 友元函数无法访问私有成员
#ifndef STRING_H
#define STRING_H
#include<iostream>
using namespace std;
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String(const char *s);
String();
String(const String &);
~String();
int length()const{return len;}
//overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
//overloaded operator friend
friend bool operator<(const String &st,const String &st2);
friend bool operator>(const String &st,const String &st2);
friend bool operator==(const String &st,const String &st2);
friend ostream & operator<<(ostream &os,const String &st);
friend istream & operator>>(istream &is,String & st);
//static function
static int HowMany();
};
#endif // STRING_H
istream & operator>>(istream & is,const String &st)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
st=temp;
while(is&&is.get()!='\n')
continue;
return is;
}
错误提示

为什么这里面友元函数不能访问私有成员?
...全文
相关推荐
VC6.0中重载操作符友元函数无法访问类的私有成员的解决办法.pdf
。
C++编程:解决无法访问私有成员的问题
在C++编程中,私有成员是类中的一种重要元素,它们用于封装数据和实现类的内部逻辑。本文将介绍一种常见的情况,即如何解决无法访问私有成员的问题,并提供相应的源代码来说明。的友元类,我们成功解决了无法访问私有成员的问题。请注意,友元关系是双向的,因此在需要访问另一个类的私有成员时,我们需要在两个类之间建立适当的友元关系。友元函数或友元类可以被允许访问类的私有成员,即使它们不是类的成员。友元关系是双向的,因此在需要访问另一个类的私有成员时,我们需要在两个类之间建立适当的友元关系。的类,其中包含一个私有成员变量。
VC6.0中重载操作符友元函数无法访问类的私有成员的解决办法.docx
。
C++ friend友元函数和友元类.docx
.
C++ friend友元函数和友元类 (2).pdf
.



