找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1560|回复: 8

[求助]肯定是有关稀构函数的调试

[复制链接]
发表于 2009-11-3 04:00:18 | 显示全部楼层 |阅读模式 IP:江苏扬州
程序如下:
#include<iostream.h>
#include<string.h>
class pica
{
public:
pica()
{
mz=new char[100];
zl=0;
}
pica(char *p,int z):zl(z)
{
strcpy(mz,p);
}
~pica()
{
delete []mz;
}
pica(const pica &other)
{
mz=new char[strlen(other.mz)+1];
strcpy(mz,other.mz);
zl=other.zl;
}
pica& operator =(const pica &other)
{
mz=new char[strlen(other.mz)+1];
strcpy(mz,other.mz);
zl=other.zl;
return *this;
}
friend ostream& operator <<(ostream &out,const pica &other)
{
out<<other.mz;
out<<other.zl;
return out;
}
friend istream& operator >>(istream &in,pica &other)
{
in>>other.mz;
in>>other.zl;
return in;
}
private:
char *mz;
int zl;
};
void main()
{
int b;
cout<<"请输入要查询几辆车"<<endl;
cin>>b;
pica* gaga=new pica[b];
for(int i=0;i<b;i++)
{
char *p=NULL;
p=new char[100];
cout<<"输入生产商"<<endl;
cin>>p;
cout<<"输入制造年份"<<endl;
int m;
cin>>m;
pica a(p,m);
gaga[i]=a;
}
for(int j=0;j<b;j++)
{
cout<<gaga[j]<<endl;
}
delete []gaga;
}
为什么运行事出错啊 一直找不到原因 我合计是NEW 和DELETE的问题 但是看不出来 希望大家指点指点吧
发表于 2009-11-3 04:00:19 | 显示全部楼层 IP:江苏扬州
以下是引用heliujin在2006-7-28 9:03:03的发言:

程序如下:
#include<iostream.h>
#include<string.h>
class pica
{
public:
pica()
{
mz=new char[100];
zl=0;
}
pica(char *p,int z):zl(z)
{
mz=new char[zl];
strcpy(mz,p);
}
~pica()
{
delete []mz;
}
pica(const pica &other)
{
mz=new char[strlen(other.mz)+1];
strcpy(mz,other.mz);
zl=other.zl;
}
pica& operator =(const pica &other)
{
mz=new char[strlen(other.mz)+1];
strcpy(mz,other.mz);
zl=other.zl;
return *this;
}
friend ostream& operator <<(ostream &out,const pica &other)
{
out<<other.mz;
out<<other.zl;
return out;
}
friend istream& operator >>(istream &in,pica &other)
{
in>>other.mz;
in>>other.zl;
return in;
}
private:
char *mz;
int zl;
};
void main()
{
int b;
cout<<"请输入要查询几辆车"<<endl;
cin>>b;
pica* gaga=new pica[b];
for(int i=0;i<b;i++)
{
char *p=NULL;
p=new char[100];
cout<<"输入生产商"<<endl;
cin>>p;
cout<<"输入制造年份"<<endl;
int m;
cin>>m;
pica a(p,m);
gaga[i]=a;
delete p;
}
for(int j=0;j<b;j++)
{
cout<<gaga[j]<<endl;
}
delete []gaga;
}
回复

使用道具 举报

发表于 2009-11-3 04:00:20 | 显示全部楼层 IP:江苏扬州
pica(char *p,int z):zl(z)
{
strcpy(mz,p);
}
MZ没空间,这里是0
非法访问
加#include<string.h>
mz=new(strlen(p)+1);
回复

使用道具 举报

发表于 2009-11-3 04:00:21 | 显示全部楼层 IP:江苏扬州
还有楼上的delete
WFPB,INT 与 CHAR* 这里没关系吧
回复

使用道具 举报

发表于 2009-11-3 04:00:22 | 显示全部楼层 IP:江苏扬州
以下是引用song4在2006-7-28 12:27:20的发言:

还有楼上的delete
WFPB,INT 与 CHAR* 这里没关系吧
song4,呵呵,你也来了啊,没看明白你这句的意思~!
回复

使用道具 举报

发表于 2009-11-3 04:00:23 | 显示全部楼层 IP:江苏扬州
char *mz;
int zl;
这里zl好象与mz没关系
pica(char *p,int z):zl(z)
{
mz=new char[zl];
strcpy(mz,p);
}
回复

使用道具 举报

发表于 2009-11-3 04:00:25 | 显示全部楼层 IP:江苏扬州
谢谢大家了 (zl和mz确实没关系)
回复

使用道具 举报

发表于 2009-11-3 04:00:26 | 显示全部楼层 IP:江苏扬州
哦。搞昏了,呵呵
pica(char *p,int z):zl(z)
    {
        mz=new char[strlen(p)+1];
        strcpy(mz,p);
    }
回复

使用道具 举报

发表于 2009-11-3 04:00:30 | 显示全部楼层 IP:江苏扬州
额!好长的程序!
回复

使用道具 举报

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

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 15:25 , Processed in 0.256920 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表