在重载<<运算符时,将 ostream声明为类的友元,提示错误无法访问类私有成员
原代码如下:
//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;
}
希望哪位高手帮帮忙看一下,谢谢!!