各位仁兄能帮我看看怎么让这个编译通过
#include<iostream>#include<cstring>
using namespace std;
class String
{
private:
char* _str;
public:
String(char*str=NULL):_str(str)//构造函数
{
if(_str!=NULL)
_str = new char[strlen(str)+1];
strcpy(_str,str);
}
String(const String& other) : _str(other._str)//拷贝构造函数
{
if (other._str != NULL)
{
_str = new char[strlen(other._str)+1];
strcpy(_str, other._str);
}
}
~String()//析构函数
{
delete[] _str;
}
public:
friend ostream& operator<<(ostream& out,String& str)//“<<”运算符重载
{
out<<str._str;
return out;
}
String operator+=(String other)//"+="运算符重载
{
int len = strlen(_str)+strlen(other._str);
if(len==0)
{
return *this;
};
char* buffer = new char[len+1];
strcpy(buffer,_str);
strcat(buffer,other._str);
delete[] _str;
_str = buffer;
return *this;
}
bool operator!=(String other)//"!="运算符重载
{
if(strcmp(_str,other._str)==0)
return true;
else
return false;
}
};
void main()
{
String a="good student";
String b="good";
b+="student";
if(a!=b)
{
cout<<"not same"<<endl;
};
char str[100];
strcpy(str,(const char*)a);//这条语句通不过,在类中加什么操作能让编译通过?
cout<<str<<endl;
}