请教为何出现段错误

tcbsyao 2008-03-29 03:50:52
1 #include <iostream>
2 using namespace std;
3 #include <string>
4
5 class String
6 {
7 char* x;
8 public:
9 String(char* x):x(0){}
10 String(const String& s){}
11 friend String operator+(const String& s2,const String& s3);
12 friend ostream& operator<<(ostream& o,const String& s1);
13
14 };
15 String operator+(const String& s2,const String& s3)
16 {
17 char* x=strcat(s2.x,s3.x);
18 return String(x);
19 }
20 ostream& operator<<(ostream& o,const String& s)
21 {
22 o<<s.x;
23 return o;
24 }
25
26 int main()
27 {
28 String string1("abc"),string2("efg"),string3("hij");
29 string1=string2+string3;
30 cout<<string1;
31 }


...全文
63 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hoohag 2008-03-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ttkk_2007 的回复:]
2楼明显不对,加完了之后,连string2都变了
m_x = new char[strlen(x)]; //m_x = new char[strlen(x) +1];
[/Quote]

呵呵, 忘记加1了. 对于上面的回复我只是为了说明构造函数, 对其他函数的功能, 没有仔细检查. 所以造成string2
的问题.
ttkk_2007 2008-03-29
  • 打赏
  • 举报
回复

class String
{
char* m_x;
public:

String(char* x){
m_x = new char[strlen(x) + 1];
strcpy(m_x, x);
}
~String(){
delete m_x;
m_x = NULL;
}
String(const String& s){
m_x = new char[strlen(s.m_x) + 1];
strcpy(m_x, s.m_x);
}
String & operator = (const String &s){
if(this == &s)
return *this;
delete m_x;
m_x = new char[strlen(s.m_x) + 1];
strcpy(m_x, s.m_x);
return *this;
}
friend String operator+(const String& s2,const String& s3);
friend ostream& operator <<(ostream& o,const String& s1);
};
String operator+(const String& s2,const String& s3)
{
char *p = new char[strlen(s2.m_x) + strlen(s3.m_x) + 1] ;
strcpy(p, s2.m_x);
strcat(p, s3.m_x);
String temp(p);
delete p;
return temp;
}
ostream& operator <<(ostream& o,const String& s)
{
o <<s.m_x;
return o;
}

int main()
{
String string1("abc"), string2("zhu"), string3("loiu");
string1 = string2 + string3;

cout << string1 << endl;

}
帅得不敢出门 2008-03-29
  • 打赏
  • 举报
回复
引用 2楼 m_x = new char[strlen(x)+1];

还有楼主没有=的操作符重载

ttkk_2007 2008-03-29
  • 打赏
  • 举报
回复
2楼明显不对,加完了之后,连string2都变了
m_x = new char[strlen(x)]; //m_x = new char[strlen(x) +1];
hoohag 2008-03-29
  • 打赏
  • 举报
回复
把你的String 的类的构造函数改一下



#include  <iostream>
using namespace std;
#include <string>

class String
{

public:
char* m_x;
String(char* x){
m_x = new char[strlen(x)];
strcpy(m_x, x);
}
String(const String& s){}
friend String operator+(const String& s2,const String& s3);
friend ostream& operator <<(ostream& o,const String& s1);
};
String operator+(const String& s2,const String& s3)
{
char* m_x = strcat(s2.m_x,s3.m_x);
return String(m_x);
}
ostream& operator <<(ostream& o,const String& s)
{
o <<s.m_x;
return o;
}

int main()
{
String string1("abc"), string2("efg"), string3("hij");
string1 = string2 + string3;
cout << string1;
system("Pause");
}
ttkk_2007 2008-03-29
  • 打赏
  • 举报
回复
String(char* x):x(0){} //x(0)??
char* x=strcat(s2.x,s3.x); //s2.x不够大,不能存储两个字符串的和
好多错误

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧