c++运算符重载问题,跪求高手解答。运行时程序中止,。请问哪里出错了?
//string2.h#include<iostream>#include<cctype>usingnamespacestd;classString{private:ch...
//string2.h
#include<iostream>
#include<cctype>
using namespace std;
class String
{private:
char *str;
int chars;
public:
String();
~String();
String(const char *s1);
String(const String &s1);
String &operator=(String &s1);
String &operator=(char *s);
int has(char s);
friend String operator+(char *s1,String &s2);
friend ostream& operator<<(ostream &os,String &s);
friend istream& operator>>(istream &is,String &s);
friend String operator+(String &s1,String &s2);
friend bool operator==(String &s1,String &s2);
void Stringup();
void Stringlow();
};
//string.cpp
#include"string2.h"
int String::has(char s){
int num=0;
for(int i=0;i<chars;i++)
if(s=str[i])
num++;
return num;
}
String::String(){str=new char[1];str='\0';}
String::~String(){delete [] str;}
String::String(const char *s1){chars=strlen(s1); str=new char[chars+1];strcpy(str,s1);}
String::String(const String &s1){str=s1.str;chars=s1.chars;}
String & String::operator=(String &s1){
if(this==&s1)
return *this;
delete [] str;
chars=s1.chars;
str=new char[chars+1];
str=s1.str;return *this;}
String & String::operator=(char *s)
{delete []str;
chars=strlen(s);
str=new char[chars+1];
strcpy(str,s);
return *this;}
ostream& operator<<(ostream &os,String &s)
{os<<s.str;return os;}
istream& operator>>(istream &is,String &s)
{char temp[50];
is.getline(temp,50);
s=temp;
return is;}
String operator+(char *s1,String &s2){s1=strcat(s1,s2.str);return s1;}
String operator+(String &s1,String &s2){s1.str=strcat(s1.str,s2.str);return s1;}
void String::Stringup()
{for(int i=0;i<chars;i++)
str[chars]=toupper(str[chars]);
}
void String::Stringlow()
{for(int i=0;i<chars;i++)
str[chars]=str[chars]=tolower(str[chars]);
}
bool operator==(String &s1,String &s2)
{if(s1.str==s2.str)
return true;
else return false;
}
main_string2.cpp
#include<iostream>
using namespace std;
#include"string2.h"
int main()
{String s1(" and I am a C++ student.");
String s2="Please enter your name:";
String s3;
cout<<s2;
cin>>s3;
s2="My name is"+s3;
cout<<s2<<".\n";
s2=s2+s1;
s2.Stringup();
cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
s1="red";
String rgb[3]={String(s1),String("green"),String("blue")};
cout<<"Enter the name of a primary color for mixing light:";
String ans;
bool success=false;
while(cin>>ans)
{ans.Stringlow();
for(int i=0;i<3;i++)
{if(ans==rgb[i])
{cout<<"That's right!\n";
success=true;
break;
}
}
if(success)
break;
else
cout<<"Try again!\n";
}
cout<<"Bye\n";
return 0;
} 展开
#include<iostream>
#include<cctype>
using namespace std;
class String
{private:
char *str;
int chars;
public:
String();
~String();
String(const char *s1);
String(const String &s1);
String &operator=(String &s1);
String &operator=(char *s);
int has(char s);
friend String operator+(char *s1,String &s2);
friend ostream& operator<<(ostream &os,String &s);
friend istream& operator>>(istream &is,String &s);
friend String operator+(String &s1,String &s2);
friend bool operator==(String &s1,String &s2);
void Stringup();
void Stringlow();
};
//string.cpp
#include"string2.h"
int String::has(char s){
int num=0;
for(int i=0;i<chars;i++)
if(s=str[i])
num++;
return num;
}
String::String(){str=new char[1];str='\0';}
String::~String(){delete [] str;}
String::String(const char *s1){chars=strlen(s1); str=new char[chars+1];strcpy(str,s1);}
String::String(const String &s1){str=s1.str;chars=s1.chars;}
String & String::operator=(String &s1){
if(this==&s1)
return *this;
delete [] str;
chars=s1.chars;
str=new char[chars+1];
str=s1.str;return *this;}
String & String::operator=(char *s)
{delete []str;
chars=strlen(s);
str=new char[chars+1];
strcpy(str,s);
return *this;}
ostream& operator<<(ostream &os,String &s)
{os<<s.str;return os;}
istream& operator>>(istream &is,String &s)
{char temp[50];
is.getline(temp,50);
s=temp;
return is;}
String operator+(char *s1,String &s2){s1=strcat(s1,s2.str);return s1;}
String operator+(String &s1,String &s2){s1.str=strcat(s1.str,s2.str);return s1;}
void String::Stringup()
{for(int i=0;i<chars;i++)
str[chars]=toupper(str[chars]);
}
void String::Stringlow()
{for(int i=0;i<chars;i++)
str[chars]=str[chars]=tolower(str[chars]);
}
bool operator==(String &s1,String &s2)
{if(s1.str==s2.str)
return true;
else return false;
}
main_string2.cpp
#include<iostream>
using namespace std;
#include"string2.h"
int main()
{String s1(" and I am a C++ student.");
String s2="Please enter your name:";
String s3;
cout<<s2;
cin>>s3;
s2="My name is"+s3;
cout<<s2<<".\n";
s2=s2+s1;
s2.Stringup();
cout<<"The string\n"<<s2<<"\ncontains "<<s2.has('A')<<" 'A' characters in it.\n";
s1="red";
String rgb[3]={String(s1),String("green"),String("blue")};
cout<<"Enter the name of a primary color for mixing light:";
String ans;
bool success=false;
while(cin>>ans)
{ans.Stringlow();
for(int i=0;i<3;i++)
{if(ans==rgb[i])
{cout<<"That's right!\n";
success=true;
break;
}
}
if(success)
break;
else
cout<<"Try again!\n";
}
cout<<"Bye\n";
return 0;
} 展开
展开全部
String & String::operator=(String &s1){
if(this==&s1)
return *this;
delete [] str;
chars=s1.chars;
str=new char[chars+1];
str=s1.str;return *this;}//这里不能直接用指针赋值,否者对象析构的时候就会导致异常,
请改为:
String & String::operator=(String &s1){
if(this==&s1)
return *this;
delete [] str;
chars=s1.chars;
str=new char[chars+1];
strcpy(str,s1.str);//改为这样
return *this;
}
Operator+的修改如下
String operator+(char *s1,String &s2){
int len = strlen(s1);
char *dest = new char[len+1];
strcpy(dest,s1);
strcat(dest,s2.str);
String tmp(dest);
delete[] dest;
return tmp;
}
String operator+(String &s1,String &s2){
char *dest = new char[s1.chars+s2.chars+1];
strcpy(dest,s1.str);
strcat(dest,s2.str);
delete[] s1.str;
s1.chars += s2.chars;
s1.str=dest;
return s1;
}
追问
还是不行啊,修改后输入名字就没反应了。求解答
2014-01-26
展开全部
内存分配问题。每次要拷贝用到的指针要保证内存分配正确。
追问
如何改正?
追答
建议注释掉一些成员函数。比如开始就只保留构造函数和析构函数。保证构造、析构没有出问题。一步一步排查。也可以调试程序。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String operator+(char *s1,String &s2)
{
s1=strcat(s1,s2.str);
return s1;
}
strcat里的s1的内存分配,
{
s1=strcat(s1,s2.str);
return s1;
}
strcat里的s1的内存分配,
追问
我不能想出其他构造方式了,能否帮改正一下?
追答
strcat使用的时候 一定要有足够的内存空间
int len_s1 = strlen(s1);
int len_s2 = strlen(s2.str);char *dest = new char[len_s1 + len_s2 + 1];strcpy(dest,s1);strcat(dest,s2.str);String tmp(dest);delete[] dest;return tmp;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询