| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 394 人关注过本帖
标题:c++程序中析构问题
只看楼主 加入收藏
lqsh
Rank: 2
来 自:山东济南
等 级:论坛游民
帖 子:26
专家分:58
注 册:2011-8-29
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
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函数老出现问题
搜索更多相关主题的帖子: void position public friend 
2011-08-29 21:42
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:20 
随手写的,你再检查一下

程序代码:
#include <iostream>

class Picture
{
public:
    explicit Picture( size_t wight=0, size_t hight=0 ) : wight_(wight), hight_(hight)
    {
        if( wight_*hight_ == 0 )
            data_ = 0;
        else
        {
            data_ = new char[wight_*hight_];
            memset( data_, 0x20, wight_*hight_*sizeof(char) );
        }
    }
    Picture( const char* buf[], size_t n )
    {
        hight_ = n;
        wight_ = 0;
        for( size_t r=0; r<hight_; ++r )
            wight_ = std::max( wight_, strlen(buf[r]) );

        if( wight_*hight_ == 0 )
            data_ = 0;
        else
        {
            data_ = new char[wight_*hight_];
            memset( data_, 0x20, wight_*hight_*sizeof(char) );
            for( size_t r=0; r<hight_; ++r )
                memcpy( &data_[r*wight_], buf[r], strlen(buf[r]) );
        }
    }
    Picture( const Picture& rhs )
    {
        wight_ = rhs.wight_;
        hight_ = rhs.hight_;
        if( rhs.data_ == 0 )
            data_ = 0;
        else
        {
            data_ = new char[wight_*hight_];
            memcpy( data_, rhs.data_, wight_*hight_*sizeof(char) );
        }
    }
    Picture& operator=( const Picture& rhs )
    {
        if( &rhs != this )
        {
            delete[] data_;

            wight_ = rhs.wight_;
            hight_ = rhs.hight_;
            if( rhs.data_ == 0 )
                data_ = 0;
            else
            {
                data_ = new char[wight_*hight_];
                memcpy( data_, 0, wight_*hight_*sizeof(char) );
            }
        }
        return *this;
    }
    ~Picture()
    {
        delete[] data_;
    }

    size_t wight() const
    {
        return wight_;
    }
    size_t hight() const
    {
        return hight_;
    }

    char* position( size_t row, size_t col )
    {
        return &data_[row*wight_+col];
    }
    const char* position( size_t row, size_t col ) const
    {
        return &data_[row*wight_+col];
    }

    void CopyFrom( size_t row_dst, size_t col_dst, const Picture& rhs )
    {
        for( size_t r=row_dst; r<hight_ && r-row_dst<rhs.hight_; ++r )
            memcpy( &data_[r*wight_+col_dst], &rhs.data_[(r-row_dst)*rhs.wight_],  std::min(wight_-col_dst,rhs.wight_) );
    }

private:
    size_t wight_, hight_;
    char* data_;
};

std::ostream& operator<<( std::ostream& os, const Picture& obj )
{
    for( size_t r=0; r<obj.hight(); ++r )
    {
        os.write( obj.position(r,0), obj.wight() );
        os<<'\n';
    }
    return os;
}

Picture frame( const Picture& p )
{
    Picture s( p.wight()+2, p.hight()+2 );
    s.CopyFrom( 1, 1, p );

    for( size_t i=1; i<s.hight()-1; ++i )
    {
        *s.position(i,0) = '|';
        *s.position(i,s.wight()-1) = '|';
    }
    for( size_t i=1; i<s.wight()-1; ++i )
    {
        *s.position(0,i) = '-';
        *s.position(s.hight()-1,i) = '-';
    }
    *s.position(0,0) = '+';
    *s.position(0,s.wight()-1) = '+';
    *s.position(s.hight()-1,0) = '+';
    *s.position(s.hight()-1,s.wight()-1) = '+';

    return s;
}

using namespace std;

int main()
{
    const char* name[] = { "abc", "defg", "higkl" };
    Picture p( name, sizeof(name)/sizeof(name[0]) );

    cout << p << endl;

    cout << frame(p) << endl;

    return 0;
}

2011-08-30 13:19
lqsh
Rank: 2
来 自:山东济南
等 级:论坛游民
帖 子:26
专家分:58
注 册:2011-8-29
收藏
得分:0 
谢谢啦,问题已经解决。
s.position(0,0)='+';
    s.position(0,s.wight-1)='+';
    s.position(s.hight-1,0)='+';
    s.position(s.hight-1,s.wight-1)='+';
position函数内的坐标写错,导致data[]越界。
上面楼主能留些qq号吗
2011-08-30 22:01
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
position函数内的坐标写错,导致data[]越界。
--- 那只是一个微不足道的小问题,否则我也不会重写你的代码了
2011-08-31 08:15
快速回复:c++程序中析构问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044032 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved