编程论坛's Archiver

shl008 发表于 2008-4-25 14:01

[紧急求助]小程序退出时出错。多谢都来帮帮忙吧……

#include<iostream.h>
#include<string.h>
#include<assert.h>
#include<iomanip.h>
#include<math.h>

#define NUM 50        //定义每个类数组的最大成员个数

class Point        //定义点类即角色
{
        public:
                void set(int xx,int yy)        //赋值坐标
                {
                        x=xx;
                        y=yy;
                }
                void setname(char *n)        //赋值名字
                {
                        name=new char[strlen(n)+1];        //申请空间
                        assert(name!=0);        //判断是否申请成功
                        strcpy(name,n);
                }
                int getx()        //返回坐标x
                {
                        return x;
                }
                int gety()        //返回坐标y
                {
                        return y;
                }
                char *getname()        //返回名字
                {
                        return name;
                }
                friend ostream & operator<<(ostream &output,Point &);        //重载流输出运算符
                virtual ~Point()        //定义虚析构函数
                {
                        delete [] name;
                }
        private:
                int x,y;
                char *name;
};
ostream & operator<<(ostream &output,Point &q)
{
        output <<"("<<q.getx()<<","<<q.gety()<<")"<<";"<<q.getname()<<endl;
        return output;
}

class Line:public Point        //定义线类即道路
{
        public:
                void set(int xx1,int yy1,int xx2,int yy2)        //赋值道路两端点坐标
                {
                        x1=xx1;
                        y1=yy1;
                        x2=xx2;
                        y2=yy2;
                }
                int getx1()        //返回坐标x1
                {
                        return x1;
                }
                int gety1()        //返回坐标y1
                {
                        return y1;
                }
                int getx2()        //返回坐标x2
                {
                        return x2;
                }
                int gety2()        //返回坐标y2
                {
                        return y2;
                }
                friend ostream & operator<<(ostream &output,Line &);        //重载流输出运算符
                ~Line(){}        //自定义析构函数
        private:
                int x1,y1,x2,y2;
};
ostream & operator<<(ostream &output,Line &q)
{
        output <<sqrt((q.getx1()-q.getx2())*(q.getx1()-q.getx2())+(q.gety1()-q.gety2())*(q.gety1()-q.gety2()))<<";"<<q.getname()<<endl;
        return output;
}

class Rectangle:public Point        //定义矩形类即小地图
{
        public:
                void setlw(int l,int w)        //赋值矩形长、宽
                {
                        length=l;
                        width=w;
                }
                int getlength()        //返回矩形长
                {
                        return length;
                }
                int getwidth()        //返回矩形宽
                {
                        return width;
                }
                friend ostream & operator<<(ostream &output,Rectangle &);        //重载流输出运算符
                ~Rectangle(){}        //自定义析构函数
        private:
                int length,width;
};
ostream & operator<<(ostream &output,Rectangle &q)
{
        output <<"("<<q.getx()<<","<<q.gety()<<")"<<";"<<q.getlength()*q.getwidth()<<";"<<q.getname()<<endl;
        return output;
}

void menu()        //用户操作选单函数
{
        cout<<"|------------------MENU------------------|"<<endl;
        cout<<"Input New Point&Output Point.............1"<<endl;
        cout<<"Input New Line&Output Line...............2"<<endl;
        cout<<"Input New Rectangle&Output Rectangle.....3"<<endl;
        cout<<"Quit.....................................0"<<endl;
}

void main()
{
        Point p[NUM];        //点类数组
        Line li[NUM];        //线类数组
        Rectangle r[NUM];        //矩形类数组
        int xx,yy,xx1,yy1,xx2,yy2,l,w;
        int c;
        int i=0,j=0,k=0;
        int sum=0;
        int a,b,d;
        char n[NUM];
        menu();
        cout<<"Please chioce:";
        cin>>c;
        while(c!=0)
        {
                switch(c)
                {
                        case 1:
                                cout<<"Enter x and y:";
                                cin>>xx>>yy;
                                if(i>NUM)
                                {
                                        cout<<"no position!"<<endl;
                                        break;
                                }
                                p[i].set(xx,yy);
                                cout<<"Enter point's name:";
                                cin>>n;
                                p[i].setname(n);
                                cout<<"completed."<<endl;
                                for(a=0;a<i+1;a++)
                                        cout<<p[a];
                                sum=sum+1;        //计算角色个数
                                cout<<"Totally "<<sum<<" persons."<<endl;
                                i=i+1;
                                break;
                        case 2:
                                cout<<"Enter x1,y1 and x2,y2:";
                                cin>>xx1>>yy1>>xx2>>yy2;
                                if(j>NUM)
                                {
                                        cout<<"no position!"<<endl;
                                        break;
                                }
                                li[j].set(xx1,yy1,xx2,yy2);
                                cout<<"Enter line's name:";
                                cin>>n;
                                li[j].setname(n);
                                cout<<"completed."<<endl;
                                for(b=0;b<j+1;b++)
                                        cout<<li[b];
                                j=j+1;
                                break;
                        case 3:
                                cout<<"Enter x and y:";
                                cin>>xx>>yy;
                                if(k>NUM)
                                {
                                        cout<<"no position!"<<endl;
                                        break;
                                }
                                r[k].set(xx,yy);
                                cout<<"Enter length and width:";
                                cin>>l>>w;
                                r[k].setlw(l,w);
                                cout<<"Enter rectangle's name:";
                                cin>>n;
                                r[k].setname(n);
                                cout<<"completed."<<endl;
                                for(d=0;d<k+1;d++)
                                        cout<<r[d];
                                k=k+1;
                                break;
                }
                menu();
                cout<<"Please chioce:";
                cin>>c;
        }

}









编译没错误。运行没错误。唯独选择0退出的时候跳出那个发送错误报告的错误对话框。这是怎么回事?多谢了。我比较急。

shl008 发表于 2008-4-25 15:44

加个自定义构造函数就OK了。问题解决。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.