自定义String类,代码哪错了
#include<iostream>using namespace std;
class String{
private:
char * m_Data;
public:
String(const char * s=NULL );
String(const String & s);
String & operator =(const char *);
friend ostream& operator <<(ostream &stream,String str);
~String();
};
String::String(const char * s)
{
if(s==NULL)
{
m_Data=new char[1];
m_Data='\0';
}
else
{
int len=strlen(s);
m_Data=new char[len+1];
strcpy(m_Data,s);
}
}
String::String(const String &s)
{
/*if(*this==s)
return s;
else
{*/
int length=strlen(s.m_Data);
m_Data=new char[length+1];
strcpy(m_Data,s.m_Data);
}
String & String::operator =(const char *s)
{
if(s==NULL)
{
m_Data=new char[1];
m_Data='\0';
}
else
{
int len=strlen(s);
m_Data=new char[len+1];
strcpy(m_Data,s);
}
}
String::~String()
{
delete [] m_Data;
}
ostream& operator <<(ostream &stream,String str)
{
stream<<str.m_Data;
return stream;
}
int main()
{
String s;
String s1("nihao!");
String s2(s1);
String s3="en!";
cout<<s1<<' '<<s2<<endl;
cout<<s3;
return 0;
}(为什么有错误)
C:\Documents and Settings\Administrator\cq.cpp(59) : error C2248: 'm_Data' : cannot access private member declared in class 'String'
C:\Documents and Settings\Administrator\cq.cpp(5) : see declaration of 'm_Data'
C:\Documents and Settings\Administrator\cq.cpp(68) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Administrator\cq.cpp(69) : error C2593: 'operator <<' is ambiguous