源代码如下,编译的时候告诉我不能访问私有成员,可是我明明定义了友元函数,不知道为什么还是错的。 5
#include<iostream>usingnamespacestd;classstringbad{private:char*str;intlen;staticintn...
#include <iostream>
using namespace std;
class stringbad
{
private:
char * str;
int len;
static int num_strings;
public:
stringbad(const char * s);
stringbad();
~stringbad();
friend ostream & operator<<(ostream & os,const stringbad & t);
};
int stringbad::num_strings=0;
stringbad::stringbad(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
cout<<num_strings<<":\""<<str<<"\" object created/n";
}
stringbad::~stringbad()
{
cout<<"\""<<str<<"\" object deleted";
--num_strings;
cout<<num_strings<<"left\n";
delete [] str;
}
ostream & operator<<(ostream & os,const stringbad & t)
{
cout<<t.len;
return os;
} 展开
using namespace std;
class stringbad
{
private:
char * str;
int len;
static int num_strings;
public:
stringbad(const char * s);
stringbad();
~stringbad();
friend ostream & operator<<(ostream & os,const stringbad & t);
};
int stringbad::num_strings=0;
stringbad::stringbad(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
cout<<num_strings<<":\""<<str<<"\" object created/n";
}
stringbad::~stringbad()
{
cout<<"\""<<str<<"\" object deleted";
--num_strings;
cout<<num_strings<<"left\n";
delete [] str;
}
ostream & operator<<(ostream & os,const stringbad & t)
{
cout<<t.len;
return os;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询