定义字符串类String,重载运算符=、+=、-=、==和!=,并在主函数中检查运算符的正确性。
以下是我的代码,不知哪里错了。调式不出来,求大神帮忙#include<iostream>#include<cstring>#include<cstdlib>#includ...
以下是我的代码,不知哪里错了。调式不出来,求大神帮忙
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<string>
using namespace std;
class String{
protected:
char*sp;
public:
String(const String&);
String(const char*s=NULL){
if(s){
sp=new char[strlen(s)+1];
strcpy(sp,s);
}else {
sp=new char[1]; //和书上不同,防止输入空串时析构函数发生错误
*sp='\0';
}
}
~String(){
delete[]sp;
}
void Show(){
if(sp)
cout<<sp<<endl;
}
String&operator=(const String&);
String&operator+=(const char*);
String&operator-=(const char*);
int operator==(const char *s) const{
return strcmp(sp,s)==0;
}
int operator!=(const char*s)const{
return strcmp(sp,s)!=0;
}
};
String& String::operator=(const String&s){
if(s.sp){
char*t=new char[strlen(s.sp)+1];
strcpy(t,s.sp);
delete[]sp;
sp=t;
}else sp=0;
return *this;
}
String& String::operator+=(const char*s2){
char*s;
s=new char[strlen(sp)+strlen(s2)+1];
strcpy(s,sp);strcat(s,s2);
delete sp;
sp=s;
return *this;
}
String&String::operator-=(const char*s2){
char*p,*s;
if(p=strstr(sp,s2)){
s=new char[strlen(sp)-strlen(s2)+1];
*p='\0';
strcpy(s,sp);
p+=strlen(s2);
strcat(s,p);
delete sp;
sp=s;
}
return *this;
}
int main(void){
String s1("C++"),s2,s3("认真学习");
s2=s1;
s2.Show();
s3+=s2;
s3.Show();
s3-=s1;
s3.Show();
s3-="认";
s3.Show();
return 0;
}
以下有这样的调试错误提示
error C2679: binary '+=' : no operator defined which takes a right-hand operand of type 'class String' (or there is no acceptable conversion)
error C2679: binary '-=' : no operator defined which takes a right-hand operand of type 'class String' (or there is no acceptable conversion)
求大神解答 展开
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<string>
using namespace std;
class String{
protected:
char*sp;
public:
String(const String&);
String(const char*s=NULL){
if(s){
sp=new char[strlen(s)+1];
strcpy(sp,s);
}else {
sp=new char[1]; //和书上不同,防止输入空串时析构函数发生错误
*sp='\0';
}
}
~String(){
delete[]sp;
}
void Show(){
if(sp)
cout<<sp<<endl;
}
String&operator=(const String&);
String&operator+=(const char*);
String&operator-=(const char*);
int operator==(const char *s) const{
return strcmp(sp,s)==0;
}
int operator!=(const char*s)const{
return strcmp(sp,s)!=0;
}
};
String& String::operator=(const String&s){
if(s.sp){
char*t=new char[strlen(s.sp)+1];
strcpy(t,s.sp);
delete[]sp;
sp=t;
}else sp=0;
return *this;
}
String& String::operator+=(const char*s2){
char*s;
s=new char[strlen(sp)+strlen(s2)+1];
strcpy(s,sp);strcat(s,s2);
delete sp;
sp=s;
return *this;
}
String&String::operator-=(const char*s2){
char*p,*s;
if(p=strstr(sp,s2)){
s=new char[strlen(sp)-strlen(s2)+1];
*p='\0';
strcpy(s,sp);
p+=strlen(s2);
strcat(s,p);
delete sp;
sp=s;
}
return *this;
}
int main(void){
String s1("C++"),s2,s3("认真学习");
s2=s1;
s2.Show();
s3+=s2;
s3.Show();
s3-=s1;
s3.Show();
s3-="认";
s3.Show();
return 0;
}
以下有这样的调试错误提示
error C2679: binary '+=' : no operator defined which takes a right-hand operand of type 'class String' (or there is no acceptable conversion)
error C2679: binary '-=' : no operator defined which takes a right-hand operand of type 'class String' (or there is no acceptable conversion)
求大神解答 展开
2个回答
2012-07-13
展开全部
+= 和 -= 操作符没有对自己定义的 String 类型重载。
String & operator+=( const String & s2 );
String & operator-=( const String & s2 );
String & String::operator+=( const String & s2 )
{
char * s = new char[ strlen( sp ) + strlen( s2.sp ) + 1 ];
strcpy( s, sp );
strcat( s, s2.sp );
delete [] sp;
sp = s;
return *this;
}
String & String::operator-=( const String & s2 )
{
char * p = strstr( sp, s2.sp );
if ( p )
{
char * s = new char[ strlen( sp ) - strlen( s2.sp ) + 1 ];
*p = '\0';
strcpy( s, sp );
p += strlen( s2.sp );
strcat( s, p );
delete sp;
sp = s;
}
return *this;
}
String & operator+=( const String & s2 );
String & operator-=( const String & s2 );
String & String::operator+=( const String & s2 )
{
char * s = new char[ strlen( sp ) + strlen( s2.sp ) + 1 ];
strcpy( s, sp );
strcat( s, s2.sp );
delete [] sp;
sp = s;
return *this;
}
String & String::operator-=( const String & s2 )
{
char * p = strstr( sp, s2.sp );
if ( p )
{
char * s = new char[ strlen( sp ) - strlen( s2.sp ) + 1 ];
*p = '\0';
strcpy( s, sp );
p += strlen( s2.sp );
strcat( s, p );
delete sp;
sp = s;
}
return *this;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我这没有编译器
你先看下每一个错误的代号所对应的是什么样的错误
然后你逐一的去搞下提示的范围实在找不出来然后我再帮你看下
你先看下每一个错误的代号所对应的是什么样的错误
然后你逐一的去搞下提示的范围实在找不出来然后我再帮你看下
追问
我那下面不是写着了嘛 不知道怎么解决而已
追答
你问题写的有问题
你写了错误代码 错误代码错在那一个区域你也没说 我这没有编译器我怎么帮你呢?
这样的话我帮不了你,不好意思。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询