error C2297: '+=' : illegal, right operand has type 'char *'为什么会出现这个错误,求解啊大神们 5
错误:d:\新建文件夹\msdev98\myprojects\1\7.cpp(31):errorC2297:'+=':illegal,rightoperandhastyp...
错误:
d:\新建文件夹\msdev98\myprojects\1\7.cpp(31) : error C2297: '+=' : illegal, right operand has type 'char *'
d:\新建文件夹\msdev98\myprojects\1\7.cpp(35) : error C2440: 'return' : cannot convert from 'char *' to 'class mystring &'
程序:
#include<iostream>
using namespace std;
class mystring
{ char*str;
public:
mystring(char*p="");
mystring(mystring&my);
~mystring();
mystring&operator+=(mystring&my);
void show();
};
mystring::mystring(char*p)
{ str=new char[strlen(p)+1];
if(str)
strcpy(str,p);
}
mystring::mystring(mystring &my)
{ str=new char[strlen(my.str)+1];
if(str)
strcpy(str,my.str);
}
mystring::~mystring()
{ if(str)
delete []str;
}
mystring&mystring::operator+=(mystring&my)
{ int len=strlen(str)+strlen(my.str)+1;
char*p=new char[len];
if(p)
{ strcpy(p,str);
p+=my.str;
}
delete []str;
str=new char[len];
return p;
}
void mystring::show()
{ cout<<str<<endl;}
int main()
{ mystring a("Abcd"),b("Defgh"),c(a);
a.show();
c.show();
a+=b;
a.show();
return 0;
} 展开
d:\新建文件夹\msdev98\myprojects\1\7.cpp(31) : error C2297: '+=' : illegal, right operand has type 'char *'
d:\新建文件夹\msdev98\myprojects\1\7.cpp(35) : error C2440: 'return' : cannot convert from 'char *' to 'class mystring &'
程序:
#include<iostream>
using namespace std;
class mystring
{ char*str;
public:
mystring(char*p="");
mystring(mystring&my);
~mystring();
mystring&operator+=(mystring&my);
void show();
};
mystring::mystring(char*p)
{ str=new char[strlen(p)+1];
if(str)
strcpy(str,p);
}
mystring::mystring(mystring &my)
{ str=new char[strlen(my.str)+1];
if(str)
strcpy(str,my.str);
}
mystring::~mystring()
{ if(str)
delete []str;
}
mystring&mystring::operator+=(mystring&my)
{ int len=strlen(str)+strlen(my.str)+1;
char*p=new char[len];
if(p)
{ strcpy(p,str);
p+=my.str;
}
delete []str;
str=new char[len];
return p;
}
void mystring::show()
{ cout<<str<<endl;}
int main()
{ mystring a("Abcd"),b("Defgh"),c(a);
a.show();
c.show();
a+=b;
a.show();
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询