C 问题为什么加上这个析构会错误

[复制链接]
查看11 | 回复4 | 2011-4-3 12:58:28 | 显示全部楼层 |阅读模式
#include iostream
#include cstring
using namespace std;
class stringg{
private:
char *ch;
public:
stringg()
{
ch = new char;
}
stringg(char *ch)
{
if(ch)
stringg::ch = new char[strlen(ch) 1];
if(stringg::ch)
strcpy(stringg::ch,ch);
}
stringg
ch = new char[strlen(temp.ch) 1];
if(ch)
strcpy(ch,temp.ch);
return *this;
}
stringg
stringg::ch = new char[strlen(ch) 1];
if(stringg::ch)
strcpy(stringg::ch,ch);
return *this;
}
friend ostream (ostream
friend istream (istream
/*~stringg()
{
if(ch)
delete [] ch;
}*/ //加上析构会错误。。。。
};
ostream (ostreamtemp.ch ;
return stream;
}
istream (istreamtemp.ch ;
return stream;
}
int main()
{

stringg a = \"312312\";

stringg b;

b = a;

coutaendlbendl;

a = \"dsaasdas\";

coutaendl ;

cinb;

coutb;

return 0;
}
还有第一个stringg(){ch = new char;} 改成stringg(){} 执行 stringg a; cina; couta;就会什么都没有
回复

使用道具 举报

千问 | 2011-4-3 12:58:28 | 显示全部楼层
<pre id=\"best-answer-content\" class=\"reply-text mb10\">你赋值的时候已经delete了ch,你在析构函数中再delete一次,应该就会出错
回复

使用道具 举报

千问 | 2011-4-3 12:58:28 | 显示全部楼层
<pre class=\"replyask-text\" id=\"content-3385136\">无语 传上来就没格式了 再说 你说的前不搭后调再说stringg::ch只是为了和ch区别开 不过还是谢谢以后把这习惯改了 还有 编译器上格式还好好的传上来就这样了
回复

使用道具 举报

千问 | 2011-4-3 12:58:28 | 显示全部楼层
<pre class=\"replyask-text\" id=\"content-3386680\">我错了,我悔过、、、
1.这是你的原码?那明确的给你说类函数的实现中出现“stringg::ch”是错的,为什么要和ch区别,根本就是同一个东西;
2.不会因为你传上来就把外部的函数写到内部来;
3.我相信你这样的代码一般人没心情来看,连个类定义的终点都要找半天,要提问就要认真点;
4.鉴于你那么毛躁,随便你说什么,我不再看这个贴。
回复

使用道具 举报

千问 | 2011-4-3 12:58:28 | 显示全部楼层
<pre class=\"replyask-text\" id=\"content-3387567\">stringg ch也可以写 stringg::ch只是说不是参数的那个ch我是菜了点也不至于你这样的 难道要写成这样?
stringg
        ch = new char[strlen(ch) 1];
        if(ch)
        strcpy(ch,ch);
        return *this;
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行