双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

C++ 友元函数无法访问私有成员

gzj_1101 gzj_1101 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#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;
}


错误提示


为什么这里面友元函数不能访问私有成员?

...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
gzj_1101
C++ Builder 13,871 成员 102,696 内容