简单的删除问题 [英] simple delete question

查看:71
本文介绍了简单的删除问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在删除我的类析构函数中new创建的一些对象,并且

它导致我的应用程序在运行时出错。下面的代码

编译好了,如果我删除

析构函数的主体也运行正常。所以我觉得我在某种程度上使用删除不正确,但是我不知道我不知道我做错了什么。


我会的感谢任何澄清和建议,以便正确地重写下面的




谢谢,

cpp


PS:下面的Char类用于存储

字符的像素表示以供显示。在这种情况下,一个角色只是一个点数集合。


#include< vector>

#include" Point.h"


class Char {

private:

char _ch;

std: :矢量<点和GT; _pixelPoints;

public:

Char(char);

~Char();

void addPoint(int ,int);

std :: vector< Point>& const getPixelPoints();

char getChar(){return _ch;}

};


Char :: Char(char ch):

_ch(ch)

{}


Char :: ~Char(){

std :: vector< Point> :: iterator iter;

for(iter = _pixelPoints.begin(); iter!= _ pixelPoints.end();

iter ++)

删除iter;

}


void Char :: addPoint(int x,int y){

_pixelPoints.push_back(*(new Point(x,y)));

}


std :: vector< Point>& ; const char :: getPixelPoints(){

返回_pixelPoints;

}

解决方案



" cppaddict" <他*** @ hello.com>在消息中写道

news:n4 ******************************** @ 4ax.com ...



我正在删除我的类析构函数中new创建的一些对象,并且它导致我的应用程序在运行时出错。下面的代码
编译好了,如果我删除
析构函数的主体也运行正常。所以我认为我在某种程度上使用删除不正确,但我不确定exaclty我做错了什么。


你做错了就是使用new和delete。目前看来这个群体不必要地使用新的流行病了。


下面的更正。

我请理解下面正确改写下面的任何说明和建议。

谢谢,
cpp

PS:下面的Char类用于存储用于显示的
字符的像素表示。在这种情况下,一个角色只是一个点的集合。

#include< vector>
#include" Point.h"

class Char {
private:
char _ch;
std :: vector< Point> _pixelPoints;
public:
Char(char);
~Char();
void addPoint(int,int);
std :: vector< Point>& ; const getPixelPoints();
char getChar(){return _ch;}
};

Char :: Char(char ch):
_ch(ch)< Char :: ~Char(){
std :: vector< Point> :: iterator iter;
for(iter = _pixelPoints.begin() ; iter!= _ pixelPoints.end();
iter ++)
删除iter;
}


不需要析构函数,删除它。

void Char :: addPoint(int x,int y){
_pixelPoints.push_back(*(new Point(x,y)));


不需要使用新的

_pixelPoints.push_back(Point(x,y));

}

std :: vector< Point>& const char :: getPixelPoints(){
返回_pixelPoints;
}




现在应该没问题。


john




" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:2h ************ @ uni-berlin.de ...


" cppaddict" ; <他*** @ hello.com>在消息中写道
新闻:n4 ******************************** @ 4ax.com ... < blockquote class =post_quotes>

我正在删除我的类析构函数中new创建的一些对象,并且它导致我的应用程序在运行时出错。下面的代码
编译好了,如果我删除
析构函数的主体也运行正常。所以我认为我在某种程度上使用删除不正确,但我不确定exaclty我做错了什么。



你做错了什么是使用新的并删除。目前似乎有一种流行病在这个群体中不必要地使用新的。




我很好奇是什么导致你使用新的。毕竟你的代码中没有

指针,所以你为什么考虑使用新的?


最近似乎发生了很多事情所以我是感兴趣的是什么使得new和

删除(以及指针一般)对新手如此有吸引力。也许你可以

解释。


john


cppaddict写道:

我正在删除我的类析构函数中new创建的一些对象,并且它导致我的应用程序在运行时出错。下面的代码
编译好了,如果我删除
析构函数的主体也运行正常。所以我想我在某种程度上使用删除不正确,但我不确定exaclty我做错了什么。




一个问题:为什么你删除你没有新的东西?


在addPoint中你添加:

*(新点(x,y))

而不是:

新点(x,y)

所以你不能删除它。


可能你可以重写你的addPoint:


void Char :: addPoint(int x,int y){

Point * p = new Point(x,y);

_pixelPoints .push_back(* p));

删除p;

}


并避免删除~Char()中的任何内容。


只需2美分......


- Dario


Hi,

I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I''m
not sure exaclty what I''m doing wrong.

I''d apprecitate any clarification and suggestions for rewriting the
below properly.

Thanks,
cpp

PS: The Char class below is used to store a pixel representation of a
character for display. A character in this context is simply a
collection of points.

#include <vector>
#include "Point.h"

class Char {
private:
char _ch;
std::vector<Point> _pixelPoints;
public:
Char(char);
~Char();
void addPoint(int, int);
std::vector<Point>& const getPixelPoints();
char getChar() {return _ch;}
};

Char::Char(char ch) :
_ch(ch)
{ }

Char::~Char() {
std::vector<Point>::iterator iter;
for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
iter++)
delete iter;
}

void Char::addPoint(int x, int y) {
_pixelPoints.push_back(*( new Point(x,y) ));
}

std::vector<Point>& const Char::getPixelPoints() {
return _pixelPoints;
}

解决方案


"cppaddict" <he***@hello.com> wrote in message
news:n4********************************@4ax.com...

Hi,

I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I''m
not sure exaclty what I''m doing wrong.
What you are doing wrong is using new and delete at all. There seems to be
an epidemic of needless use of new on this group at the moment.

Corrections below.

I''d apprecitate any clarification and suggestions for rewriting the
below properly.

Thanks,
cpp

PS: The Char class below is used to store a pixel representation of a
character for display. A character in this context is simply a
collection of points.

#include <vector>
#include "Point.h"

class Char {
private:
char _ch;
std::vector<Point> _pixelPoints;
public:
Char(char);
~Char();
void addPoint(int, int);
std::vector<Point>& const getPixelPoints();
char getChar() {return _ch;}
};

Char::Char(char ch) :
_ch(ch)
{ }

Char::~Char() {
std::vector<Point>::iterator iter;
for (iter=_pixelPoints.begin(); iter!=_pixelPoints.end();
iter++)
delete iter;
}
Don''t need the destructor, remove it.

void Char::addPoint(int x, int y) {
_pixelPoints.push_back(*( new Point(x,y) ));
Don''t need to use new

_pixelPoints.push_back(Point(x,y));
}

std::vector<Point>& const Char::getPixelPoints() {
return _pixelPoints;
}



Should be fine now.

john



"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...


"cppaddict" <he***@hello.com> wrote in message
news:n4********************************@4ax.com...

Hi,

I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I''m
not sure exaclty what I''m doing wrong.



What you are doing wrong is using new and delete at all. There seems to be
an epidemic of needless use of new on this group at the moment.



I''m curious as to what led you to use new at all. After all there are no
pointers in your code, so why did you consider using new?

Seems to be happening a lot lately so I''m interested in what makes new and
delete (and pointers generally) so attractive to newbies. Maybe you can
explain.

john


cppaddict wrote:

I am deleting some objects created by new in my class destructor, and
it is causing my application to error at runtime. The code below
compiles ok, and also runs fine if I remove the body of the
destructor. So I think I am somehow using delete incorrectly, but I''m
not sure exaclty what I''m doing wrong.



A question: Why you "delete" something that you do not "new" ?

In addPoint you add:
*( new Point(x,y) )
and not:
new Point(x,y)
so you cannot delete it.

Probably you can rewrite your addPoint as:

void Char::addPoint(int x, int y) {
Point * p = new Point(x,y);
_pixelPoints.push_back(*p));
delete p;
}

and avoid to delete anything in ~Char().

Just my 2 cents...

- Dario


这篇关于简单的删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆