免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3633 | 回复: 2
打印 上一主题 下一主题

简单内存清理问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-23 10:31 |只看该作者 |倒序浏览
向量vector在多重循环中运用,push_back()。当没有外层k循环时输出正常。当有外层k循环时,则没有这行输出 cout<<"poi"<<poi.GetY();。我怀疑是因为poi内存清理的问题。但是新手还不是很懂,怎么解决呢。

#include<iostream>
#include<stdio.h>
#include"point.h"
#include<vector>
#include<iomanip>
using namespace std;
int main()
{
for(int k=1;k<5;k++){

Point poi;
Point p1;
int j;
vector<Point> pointarry;
p1.Move(5,10);
cout<<p1.GetX()<<endl;
for(int i;i<10;i++){
j=i+30;
poi.Move(i,j);
cout<<"poi"<<poi.GetY();
pointarry.push_back(poi);
}
cout<<endl;

}
}

论坛徽章:
0
2 [报告]
发表于 2011-08-23 10:45 |只看该作者
以下为include的point.h文件


//#ifndef  _pointhh_
//#define  _pointhh_
//#include<iostream>
//using namespace std;
#include<stdio.h>
using namespace std;
class Point
{
public:

          Point()
          {X=Y=0;cout<<"Default constructor "<<endl;
          }
          Point(int xx, int yy)
          {X=xx, Y=yy;
           cout<<"Constructor called"<<endl;

          }
          int GetX(){ return X;}
          int GetY(){ return Y;}
          void Move(int x,int y)
          {X=x; Y=y;}

private:
          int X,Y;       

};

论坛徽章:
0
3 [报告]
发表于 2011-08-23 11:08 |只看该作者
将变量的定义放到循环体外部去
  1. Point poi;
  2. Point p1;
  3. int j;
  4. vector<Point> pointarry;
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP