构造函数问题(再次) [英] Constructor question (again)

查看:88
本文介绍了构造函数问题(再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让这个拷贝构造函数起作用。我不知道

为什么会崩溃。


graphic :: graphic(const graphic& gr){

ud = lr = 16;

gdata = gr.gdata; < - 此行崩溃

BITMAP位图= {0,ud,lr,2,1,1};

bitmap.bmBits =& gdata [0] ;

hbitmap = CreateBitmapIndirect(& bitmap);

}

我正在使用这样的函数:


if(play){

cgr = new graphic(play-> gOut());

return * cgr;

}

班级玩家{


字符串名称;

图形gr;

void create ();


public:

player();

graphic gOut(){return gr;}

void dummy(){MessageBox(NULL," Dummy"," Notice",MB_OK); }


};

I have been trying to get this copy constructor to work. I don''t know
why it crashes.

graphic::graphic(const graphic& gr){
ud = lr = 16;
gdata = gr.gdata; <-- This line crashes
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}
I am using the function like this:

if(play){
cgr = new graphic(play->gOut());
return *cgr;
}
class player{

string name;
graphic gr;
void create();

public:
player();
graphic gOut(){return gr;}
void dummy(){MessageBox(NULL, "Dummy" , "Notice", MB_OK); }

};

推荐答案

" JoeC" <恩***** @ yahoo.com> schrieb im Newsbeitrag新闻:11 ********************** @ g10g2000cwb.googlegr oups.com ...
"JoeC" <en*****@yahoo.com> schrieb im Newsbeitrag news:11**********************@g10g2000cwb.googlegr oups.com...
我一直在尝试让这个拷贝构造函数工作。我不知道为什么它会崩溃。

graphic :: graphic(const graphic& gr){
ud = lr = 16;
gdata = gr .gdata; < - 此行崩溃
BITMAP位图= {0,ud,lr,2,1,1};
bitmap.bmBits =& gdata [0];
hbitmap = CreateBitmapIndirect (& bitmap);
}
I have been trying to get this copy constructor to work. I don''t know
why it crashes.

graphic::graphic(const graphic& gr){
ud = lr = 16;
gdata = gr.gdata; <-- This line crashes
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}




如何定义gdata以及它的赋值运算符是什么样的?


Heinz



How is gdata defined and how does its assignment operator look like?

Heinz


JoeC写道:
我一直试图让这个拷贝构造函数起作用。我不知道为什么它会崩溃。


我们也不是。确保


1)复制该对象在语义上是有意义的

2)你正确使用你的库

3)三个规则得到尊重(任何一个{析构函数,

赋值运算符,复制构造函数}通常需要所有3个)

4)一切都很好*之前*崩溃的行(未定义

行为)

graphic :: graphic(const graphic& gr){
ud = lr = 16;
gdata = gr.gdata; < - 此行崩溃
BITMAP位图= {0,ud,lr,2,1,1};
bitmap.bmBits =& gdata [0];
hbitmap = CreateBitmapIndirect (& bitmap);
I have been trying to get this copy constructor to work. I don''t know
why it crashes.
We don''t either. Make sure

1) it makes sense semantically to copy that object
2) you are using your library correctly
3) the rule of three is respected ("A class with any of {destructor,
assignment operator, copy constructor} generally needs all 3")
4) everything is fine *before* the line where it crashes (undefined
behavior)
graphic::graphic(const graphic& gr){
ud = lr = 16;
gdata = gr.gdata; <-- This line crashes
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);




尽可能使用初始化列表:


graphic :: graphic(const graphic& gr )

:ud(16),lr(16)....


由于你提供的代码不足以诊断错误,我

无法帮助你。但是,我怀疑问题来自你使用微软的GDI库做什么。试试微软

新闻组

http://www.parashift.com/c++-faq-lit....html#faq-5.9)

Jonathan



Use initialization lists when you can:

graphic::graphic(const graphic& gr)
: ud(16), lr(16) ....

Since the code you provided is not sufficient to diagnose the error, I
cannot help you more. However, I suspect the problem comes from what
you are doing with Microsoft''s GDI library. Try in a microsoft
newsgroup
(http://www.parashift.com/c++-faq-lit....html#faq-5.9).
Jonathan


JoeC写道:
我一直试图让这个拷贝构造函数起作用。我不知道为什么它会崩溃。


我们也不知道它为什么会崩溃。

graphic :: graphic(const graphic& gr){
ud = lr = 16;
gdata = gr.gdata; < - 这一行崩溃


看来'gr''在某种程度上是无效的。什么''gdata''

包含什么?这里好吗?你是如何得到这个推荐的?

BITMAP位图= {0,ud,lr,2,1,1};
bitmap.bmBits =& gdata [0];
hbitmap = CreateBitmapIndirect(& bitmap);
}

我正在使用这样的函数:

if(play){
cgr =新图形(play-> gOut());


那么,什么'播放'?该指针是OK还是无效?

将其与零进行比较并不足以验证它。谁?
创造了它?谁填充(设置)它?可能是玩吗?在达到这一点之前的某个时刻处理了




您需要调试程序并确保在程序时

到达这一点,''play''是有效的(指向常规''玩家''

对象,仍然活着,所有字段仍然有效)。我们不能

为你做这件事。

返回* cgr;
}

班级玩家{

字符串名称;
图形gr;
void create();

public:
player();
graphic gOut(){返回gr;}
void dummy(){MessageBox(NULL," Dummy"," Notice",MB_OK); }

};
I have been trying to get this copy constructor to work. I don''t know
why it crashes.
We don''t know why it crashes either.

graphic::graphic(const graphic& gr){
ud = lr = 16;
gdata = gr.gdata; <-- This line crashes
It would seem that ''gr'' is somehow invalid here. What does ''gdata''
contain? Is ''gr'' OK here? How did you obtain this refernece?
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}
I am using the function like this:

if(play){
cgr = new graphic(play->gOut());
So, what''s "play"? Is that pointer OK or is it also invalid?
Comparing it to zero is not necessarily enough to validate it. Who
creates it? Who fills (sets) it? Could it be that "play" has been
disposed of at some point before reaching this point?

You need to debug your program and make sure that when the program
gets to this point, ''play'' is valid (points to a regular ''player''
object, still alive, with all fields still valid as well). We can''t
do it for you.
return *cgr;
}
class player{

string name;
graphic gr;
void create();

public:
player();
graphic gOut(){return gr;}
void dummy(){MessageBox(NULL, "Dummy" , "Notice", MB_OK); }

};




V

-

请删除资金' 'A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问



V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于构造函数问题(再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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