标题:程序最后运行不了,但编译没有显示错误,麻烦大家帮忙看看哪里出问题了
只看楼主
mosquiyan
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2014-12-12
结帖率:85.71%
 问题点数:0 回复次数:2 
程序最后运行不了,但编译没有显示错误,麻烦大家帮忙看看哪里出问题了
#include <iostream>
#include <cassert>
using namespace std;

class Point
{
    public:
        Point():x(0),y(0){
            cout<<"Default Constructor called."<<endl;
        }//默认构造函数
        
        Point(int x,int y):x(x),y(y){
            cout<<"Constructor called."<<endl;
        }//构造函数
        
        ~Point(){
            cout<<"Destructor called."<<endl;
        }
        
        int getX() const{
            return x;
        }
        
        int getY() const{
            return y;
        }
        
        void move(int newX,int newY){
            x=newX;
            y=newY;
        }
    private:
        int x,y;
   
};

class ArrayOfPoints{//动态数组类
  public:
       ArrayOfPoints(const ArrayOfPoints &v);
     ArrayOfPoints(int size):size(size){
     points=new Point[size];
     }
     ~ArrayOfPoints(){
         cout<<"Deleting...."<<endl;
         delete []points;
     }
     //获得下标为index的数组元素
     Point &element(int index){
         assert (index>0 && index<size);
         return points[index];
     }
  private:
      Point *points;
      int size;
     };

ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints &v){
    size=v.size;
    points=new Point[size];
    for(int i=0;i<size;i++)
      points[i]=v.points[i];
}
     
int main()
{
    int count;
    cout<<"Please enter the count of points:";
    cin>>count;
   
    ArrayOfPoints pointsArray1(count);//创建对象数组
    pointsArray1.element(0).move(5,10);
    pointsArray1.element(1).move(15,20);
   
    ArrayOfPoints pointsArray2=pointsArray1;//创建对象数组副本
    cout<<"Copy of pointsArray1:"<<endl;
    cout<<"Point_0 of array2:"<<pointsArray2.element(0).getX()<<","<<pointsArray2.element(0).getY()<<endl;
    cout<<"Point_1 of array2:"<<pointsArray2.element(1).getX()<<","<<pointsArray2.element(1).getY()<<endl;
   
    pointsArray1.element(0).move(25,30);
    pointsArray1.element(1).move(35,40);
   
    cout<<"After the moving of pointsArray:"<<endl;
    cout<<"Point_0 of array2:"<<pointsArray2.element(0).getX()<<","<<pointsArray2.element(0).getY()<<endl;
    cout<<"Point_1 of array2:"<<pointsArray2.element(1).getX()<<","<<pointsArray2.element(1).getY()<<endl;
   
    return 0;
}
搜索更多相关主题的帖子: include public return called 
2015-04-21 13:49
小狼烟
Rank: 2
等 级:论坛游民
威 望:1
帖 子:15
专家分:32
注 册:2011-11-23
得分:0 
  //获得下标为index的数组元素
     Point &element(int index){
         assert (index>0 && index<size);
         return points[index];
     }

这个地方的判断ti条件不对吧,你第一次index位置就是0,但是你用assert()判断不符合就终止程序了。。。
改成:
  //获得下标为index的数组元素
     Point &element(int index){
         assert (index>=0 && index<=size);
         return points[index];
     }
就可以了吧

啦啦啦德玛西亚!
2015-04-21 17:33
mosquiyan
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2014-12-12
得分:0 
回复 2楼 小狼烟
恩恩 刚刚发现了 谢谢哈
2015-04-21 18:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-444330-1-1.html




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

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