在重载<<运算符时,将 ostream声明为类的友元,提示错误无法访问类私有成员

ChrisXiaoxh 2007-12-02 09:09:27
原代码如下:
//string1.h
#ifndef STRING1_H
#define STRING1_H

const int maxLen=300; //字符串的最大长度
enum Boolen{False,True};

class string1;

template <class T>
class ListNode
{
friend class string1;
public:
ListNode():link(NULL){}
ListNode(T d,ListNode<T> * n=NULL):data(d),link(n){}
private:
T data;
ListNode<T> * link;
};

class string1
{
public:
string1();
string1(char *obstr);
~string1();
int Length() const {return curLen;}
string1 &operator =(string1 & ob);
char operator [](int);
Boolen operator ==(string1 & ob);
string1 operator()(int pos,int len); //取子串
string1 & operator += (string1 & ob);
int Find(string1 & ob); //求子串在字符串中的位置
friend ostream & operator << (ostream & os,const string1 & ob);
friend istream & operator >> (istream & is,string1 & ob);
private:
ListNode<char> * chList;
int curLen;
};

#endif

//string1.cpp
#include <iostream.h>
#include "string1.h"

//using namespace std;

string1::string1()
{
chList=new ListNode<char>('\0');
curLen=0;
}

string1::string1(char * obstr)
{
ListNode<char> *p=chList;
while(*obstr!='\0')
{
p=p->link=new ListNode<char>(*obstr);
obstr++;
curLen++;
}
}

string1 & string1::operator =(string1 & ob)
{
curLen=ob.curLen;
ListNode<char> *p=chList->link,*q=ob.chList->link;
while(q!=NULL)
{
p=p->link=new ListNode<char>(q->data);
}
p->link=new ListNode<char>('\0');
return *this;
}

char string1::operator [] (int i)
{
if(i>=0&&i<curLen)
{
int k=0;
ListNode<char> *p=chList;
while(p!=NULL&&k<i)
{
p=p->link;
k++;
}
if(p!=NULL)
return p->data;
}
return '\0';
}

Boolen string1::operator == (string1 & ob)
{
if(curLen!=ob.curLen)
{
return False;
}
ListNode<char> *p=chList->link;
ListNode<char> *q=ob.chList->link;
while(p!=NULL&&q!=NULL)
{
if(p->data!=q->data)
return False;
p=p->link;
q=q->link;
}
return True;
}

string1 & string1::operator += (string1 & ob)
{
curLen+=ob.curLen;
ListNode<char> *p=chList->link,*q=ob.chList->link;
while(p!=NULL)
p=p->link;
while(q!=NULL)
{
p=p->link=q;
q=q->link;
}
return *this;
}

string1 string1::operator () (int pos,int len)
{
int k=0;
ListNode<char> *p=chList;
string1 temp;
ListNode<char> *q=temp.chList;
if(pos>=0&&len>=0&&pos+len-1<curLen&&pos<curLen)
{
while(k<pos)
{
p=p->link;
k++;
}
int i=0;
while(i<len)
{
q=q->link=new ListNode<char> (p->data);
p=p->link;
}
q->link=new ListNode<char>('\0');
temp.curLen=len;
}
else
{
temp.curLen=0;
temp.chList=new ListNode<char>('\0');
}
return temp;
}

int string1::Find(string1 & ob)
{
int oblen=ob.curLen;
string1 temp=*this;
int i=1;
while(i<=curLen)
{
if(temp(i,oblen)==ob)
break;
else
i++;
}
return i;
}

ostream & operator << (ostream & os,const string1 & ob)
{
cout<<"\nThe list is:";
ListNode<char> *p=ob.chList->link;
while(p!=NULL)
{
os<<p->data;
if(p->link!=NULL)
cout<<"->";
p=p->link;
}
return os;
}

istream & operator >> (istream & is,string1 & ob)
{
int count=0;
char ch;
ListNode<char> *p;
ob.chList=new ListNode<char> ('\0');
p=ob.chList;
while(is>>ch)
{
p=p->link=new ListNode<char>(ch);
count++;
}
p->link=new ListNode<char>('\0');
ob.curLen=count;
return is;
}

int main()
{
string1 strA,strB;
cin>>strA;
cout<<strA;
return 0;
}


希望哪位高手帮帮忙看一下,谢谢!!
...全文
145 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ryfdizuo 2007-12-02
  • 打赏
  • 举报
回复
输入输出没有问题了, 增加了析构函数;
#ifndef STRING1_H
#define STRING1_H
#include <iostream>
using namespace std;

const int maxLen=300; //字符串的最大长度
enum Boolen{False,True};

class string1;

template <class T>
class ListNode
{
friend class string1;
public:
ListNode():link(NULL){}
ListNode(T d,ListNode <T> * n=NULL):data(d),link(n){} ;
friend ostream & operator << (ostream & os,const string1 & ob);
friend istream & operator >> (istream & is,string1 & ob);
private:
T data;
ListNode <T> * link;
};

class string1
{
public:
string1();
string1(char *obstr);
~string1();
int Length() const {return curLen;}
string1 &operator =(string1 & ob);
char operator [](int);
Boolen operator ==(string1 & ob);
string1 operator()(int pos,int len); //取子串
string1 & operator += (string1 & ob);
int Find(string1 & ob); //求子串在字符串中的位置
friend ostream & operator << (ostream & os,const string1 & ob);
friend istream & operator >> (istream & is,string1 & ob);
private:
ListNode <char> * chList;
int curLen;
};
#endif

//main.cpp

//string1.h
//string1.cpp
#include <iostream>
#include <string>
#include "string1.h"

using namespace std;

string1::string1()
{
chList=new ListNode <char> ('\0');
curLen=0;
}

string1::string1(char * obstr)
{
ListNode <char> *p=chList;
while(*obstr!='\0')
{
p=p-> link=new ListNode <char> (*obstr);
obstr++;
curLen++;
}
}

string1 & string1::operator =(string1 & ob)
{
curLen=ob.curLen;
ListNode <char> *p=chList-> link,*q=ob.chList-> link;
while(q!=NULL)
{
p=p-> link=new ListNode <char> (q-> data);
}
p-> link=new ListNode <char> ('\0');
return *this;
}

char string1::operator [] (int i)
{
if(i>=0&&i <curLen)
{
int k=0;
ListNode <char> *p=chList;
while(p!=NULL&&k <i)
{
p=p-> link;
k++;
}
if(p!=NULL)
return p-> data;
}
return '\0';
}

Boolen string1::operator == (string1 & ob)
{
if(curLen!=ob.curLen)
{
return False;
}
ListNode <char> *p=chList-> link;
ListNode <char> *q=ob.chList-> link;
while(p!=NULL&&q!=NULL)
{
if(p-> data!=q-> data)
return False;
p=p-> link;
q=q-> link;
}
return True;
}

string1 & string1::operator += (string1 & ob)
{
curLen+=ob.curLen;
ListNode <char> *p=chList-> link,*q=ob.chList-> link;
while(p!=NULL)
p=p-> link;
while(q!=NULL)
{
p=p-> link=q;
q=q-> link;
}
return *this;
}

string1 string1::operator () (int pos,int len)
{
int k=0;
ListNode <char> *p=chList;
string1 temp;
ListNode <char> *q=temp.chList;
if(pos>=0&&len>=0&&pos+len-1 <curLen&&pos <curLen)
{
while(k <pos)
{
p=p-> link;
k++;
}
int i=0;
while(i <len)
{
q=q-> link=new ListNode <char> (p-> data);
p=p-> link;
}
q-> link=new ListNode <char> ('\0');
temp.curLen=len;
}
else
{
temp.curLen=0;
temp.chList=new ListNode <char> ('\0');
}
return temp;
}

int string1::Find(string1 & ob)
{
int oblen=ob.curLen;
string1 temp=*this;
int i=1;
while(i <=curLen)
{
if(temp(i,oblen)==ob)
break;
else
i++;
}
return i;
}
string1::~string1()
{
ListNode<char> *q ;
while(chList != NULL)
{
q=chList;
chList = q->link;

delete q;
}
}

ostream& operator<<(ostream & os,const string1 &ob)
{
cout <<"\nThe list is:";
ListNode <char>*p = ob.chList-> link;
while(p !=NULL)
{
os << p->data;
if(p->link!=NULL)
cout <<"->";
p = p->link;
}
os << endl;
return os;
}

istream & operator >> (istream & is,string1 & ob)
{
int count=0;
char ch;
string line;
ListNode <char> *p;
ob.chList=new ListNode <char> ('\0');
p=ob.chList;

getline(is, line, '\n'); //以\n结束行
for (int i=0; i<line.size(); i++)
{
p = p->link=new ListNode<char> (line.at(i));
}
count = line.size(); //大小;

//p-> link=new ListNode <char> ('\0'); //没有意义啊;
p->link = NULL;
ob.curLen=count;
return is;
}

int main()
{
string1 strA,strB;
cin>> strA;
cout <<strA;
return 0;
}
ryfdizuo 2007-12-02
  • 打赏
  • 举报
回复
友员申明在ListNode类里面也应该添加;否则无法访问ListNode类的私有成员
friend ostream & operator << (ostream & os,const string1 & ob);
friend istream & operator >> (istream & is,string1 & ob);
t88266236 2007-12-02
  • 打赏
  • 举报
回复
你这个友元是string1的,与ListNode没关系,而你现在是在访问ListNode 的数据,所以会出错。
ryfdizuo 2007-12-02
  • 打赏
  • 举报
回复
string1.h里面要加
#include <iostream>
using namespace std;
从蓝田到元谋 2007-12-02
  • 打赏
  • 举报
回复
vc6对友元的支持有问题

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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