c++程序中析构问题
class Picture{
public:
Picture():wight(0),hight(0),data(0)
{
}//对象数组用
Picture(const char *const *,int );//构造实际需求对象
Picture &operator=(const Picture &);
Picture(const Picture &);
~Picture();
//int max1(int w,int h);
char &position(int w,int h);//可作为左值
char position(int w,int h)const;//静态成员函数与上面的position函数存储在不同的代码区
void init(int w,int h);
friend void fram(const Picture &);
friend Picture operator|(const Picture &,const Picture &);
friend Picture operator&(const Picture &,const Picture &);
friend ostream & operator<<(ostream &,const Picture &);//运算符重载
private:
//protected:
int wight,hight;//像素的宽度和高度
char *data;//储存字符涉及到指针。需要拷贝构造函数,析构函数
void copyPicture(int ,int ,const Picture &);
};
char &Picture::position(int w,int h)
{
return data[w*wight+h];//验证信息
}//可做为左值向data数组存入字符
char Picture::position(int w,int h)const
{
return data[w*wight+h];
}//只能const Picture对象调用返回原来字符数组的副本
void Picture::init(int w,int h)
{
wight=w;
hight=h;
data=new char[wight*hight+1];//?有必要扩充1字符串结束标志
}
Picture::Picture(const char *const *s,int n )
{
int m=0;
const char*p=NULL;
int i,j;
for( i=0;i<n;i++)
{
int len=strlen(s[i]);
m=max(m,len);
}//找到最大的值
init(m,n);
for(i=0;i<n;i++)
{
p=s[i];
for(j=0;j<strlen(p);j++)
{
position(i,j)=p[j];
}
while(j<wight)
{
Picture::position(i,j)=' ';//不足的用空格表示
++j;
}
}
}
void Picture::copyPicture(int w ,int h ,const Picture &p)//复制起点
{
int i,j;
for(i=0;i<p.hight;i++)
{
for(j=0;j<p.wight;j++)
{
position(i+h,j+w)=p.position(i,j);
}
}
}
Picture::Picture(const Picture &p):wight(p.wight),hight(p.hight),data(new char[p.wight*p.hight])
{
Picture::copyPicture(0,0,p);
}
Picture &Picture::operator=(const Picture &p)
{
if(this!=&p)
{
delete[]data;
Picture::init(p.wight,p.hight);
Picture::copyPicture(0,0,p);
}
return *this;
}
Picture::~Picture()
{
delete[]data;
}
ostream & operator<<(ostream &o,const Picture &p)
{
int i,j;
for(i=0;i<p.hight;i++)
{
for(j=0;j<p.wight;j++)
{
o<<p.position(i,j);
}
o<<endl;
}
return o;
}
void fram(const Picture &p)
{
Picture s;
int i,j;
s.init(p.wight+2,p.hight+2);
for(i=1;i<s.hight-1;i++)
{
s.position(i,0)='|';
s.position(i,s.wight-1)='|';
}
for(j=1;j<s.wight-1;j++)
{
s.position(0,j)='-';
s.position(s.hight-1,j)='-';
}
s.position(0,0)='+';
s.position(0,s.hight-1)='+';
s.position(s.wight-1,0)='+';
s.position(s.wight-1,s.hight-1)='+';
s.copyPicture(1,1,p);
cout<<s<<endl;
//return s;
}
//int &p(int n,int *num)
//{
// return num[n];
//
//}
int main()
{
char *name[]={"abc","defg","higkl"};
Picture p(name,3);
cout<<p<<endl;
fram(p);
return 0;
}//测试代码
为什么调用fram函数老出现问题