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

gzj_1101 2016-07-06 09:44:35

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


错误提示


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

...全文
419 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
银点 2016-07-12
  • 打赏
  • 举报
回复
没问题
gzj_1101 2016-07-07
  • 打赏
  • 举报
回复
晕,今天重试了一下之后发现有没有问题
gzj_1101 2016-07-07
  • 打赏
  • 举报
回复
引用 1 楼 dataxdata 的回复:
试了一下,在CB下编译是没问题的,你用的是什么编译器?
cb啊
dataxdata 2016-07-06
  • 打赏
  • 举报
回复
试了一下,在CB下编译是没问题的,你用的是什么编译器?

13,871

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧