總是提示:Thread 1: signal SIGABRT
C++12.2(3458,0x1000d9dc0) malloc: *** error for object 0x100611dec: pointer being freed was not allocated
C++12.2(3458,0x1000d9dc0) malloc: *** set a breakpoint in malloc_error_break to debug
(lldb)
//頭檔案
ifndef C__12_hpp
#define C__12_hpp
#include <iostream>
using std::ostream;
using std::istream;
class String
{
private:
char * str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String (const String &);
~String();
String & operator=(const String &);
String & operator=(const char *);
void stringlow();
void stringup();
int has(char c);
friend String operator+(const String & s,const String & t);
bool operator==(const String & s)const;
friend std::ostream & operator<<(std::ostream & os,const String & s);
friend std::istream & operator>>(std::istream & is,String & s);
};
#endif /* C__12_hpp */
//定義
#include "C++12.hpp"
#include <cstring>
int String::num_strings=0;
String::String(const char * s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
++num_strings;
}
String::String()//修改
{
len = 0;
str=nullptr;
num_strings++;
}
String::String (const String & s)
{
++num_strings;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
}
String::~String()
{
--num_strings;
delete []str;
}
String & String::operator=(const String &s)
{
if(this==&s)
return *this;
delete []str;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
String &String:: operator=(const char * s)
{
delete[]str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
bool String::operator==(const String & s)const
{
return (strcmp(str,s.str)==0);
}
ostream & operator<<(ostream & os,const String &s)
{
os<<s.str;
return os;
}
istream & operator>>(istream & is,String &s)
{
char temp[String::CINLIM];
is.get(temp,String::CINLIM);
if(is)
s=temp;
while(is&&is.get()!='\n')
continue;
return is;
}
void String::stringup()
{
for(int i=0;i<len;++i)
str[i]=toupper(str[i]);
}
void String::stringlow()
{
for(int i=0;i<len;++i)
str[i]=tolower(str[i]);
}
int String::has(char c)
{
int m=0;
while(*str++)
if(*str==c)
++m;
return m;
}
String operator+(const String & s,const String & t)
{
String m;
m.len=s.len+t.len;
m.str=new char[m.len+1];
strcat(m.str,s.str);
strcat(m.str,t.str);
return m;
}
//主檔案
#include <iostream>
using namespace std;
#include "C++12.hpp"
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 for mixing hight: ";
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;
}
uj5u.com熱心網友回復:
先在這里加一下memsetString operator+(const String & s,const String & t)
{
String m;
m.len=s.len+t.len;
m.str=new char[m.len+1];
memset(m.str,0,m.len+1);
strcat(m.str,s.str);
strcat(m.str,t.str);
return m;
}
要有別的問題,你再看
uj5u.com熱心網友回復:
試了下還是這樣uj5u.com熱心網友回復:
has成員函式把成員指標變數str改了,析構的時候當然會例外。
int String::has(char c)
{
int m=0;
while(*str++)
if(*str==c)
++m;
return m;
}
善用const,int has(char c) const ,有助于及時定位錯誤
uj5u.com熱心網友回復:
精準!uj5u.com熱心網友回復:
謝謝轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/50742.html
標籤:C++ 語言
