473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Constructor question (again)

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); }

};

May 17 '06 #1
11 1335
"JoeC" <en*****@yahoo.com> schrieb im Newsbeitrag news:11**********************@g10g2000cwb.googlegr oups.com...
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);
}


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

Heinz
May 17 '06 #2
JoeC wrote:
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);


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

May 17 '06 #3
JoeC wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #4
Sorry, here is the header for graphic:

class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
std::vector<BYTE> gdata;
HBITMAP hbitmap;
HDC hdc, hdcmem;
public:
graphic();
graphic(const BYTE c[]);
graphic(const graphic&);
void set(const BYTE c[]);
BYTE getData(const int n)const {return gdata[0];}
void display(HWND,const int, const int);

};

May 17 '06 #5
gdata is a vector. When I comment out the line the program works but I
get a block instead of my graphic. There seems to be a problem
transfering the graphic data in the vector.

May 17 '06 #6
If there is no player then it will display a different graphic here is
the rest of the code:

graphic& space::graphicOut(){

if(play){
cgr = new graphic(play->gOut());
return *cgr;
}
if(seen){return *gr;}
else {return *grDefault;}
}
Don't worry, every thing else works. I have gotten this to work by
creating the graphic in this object. I did it like this:

graphic& space::graphicOut(){
//if(play){return play->gOut();} <-This is the way I want to do
the graphic
//but it crashes.
if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}

May 17 '06 #7
JoeC wrote:
gdata is a vector. When I comment out the line the program works but I
get a block instead of my graphic. There seems to be a problem
transfering the graphic data in the vector.


Please learn to quote correctly on Usenet. See
http://en.wikipedia.org/wiki/Top-posting#Inline_replies.

I reproduce and simplify your code here. You seem to think the problem
comes from the marked line:

graphic
{
public:
graphic(const graphic& gr)
{
gdata = gr.gdata; // <---- This line crashes
}

std::vector<BYTE> gdata;
};

If BYTE is a typedef for an unsigned char (or some builtin type), this
line has no reason to crash. It may throw an exception (for memory
allocation problems), but it cannot just "crash" (assuming the library
itself has no bug).

If BYTE is not a builtin type, then it may be its operator= or
something in that class that cause problems.

Having said that, I doubt that the statement itself causes a problem. I
suspect memory gets corrupted before that line. Again:

1) it makes sense semantically to copy that object
2) you are using your library correctly (GDI)
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)
Jonathan

May 17 '06 #8
Here is more code:

class space{
char gchar;
graphic *gr;
graphic *grDefault;
graphic * cgr;
player * play;
bool seen;
void cleanup(){cgr = 0;}

public:
space();
~space();
void graphicIn(char g);
graphic& graphicOut();
void playIn(player*);
bool isPlay();
void see(){seen = true;}
bool been(){return seen;}
void playOut();
bool canMove();
bool winspace();
};
void space::playIn(player *p){
play = p;
seen = true;
}

May 17 '06 #9
JoeC wrote:
Here is more code:

[..]


Do you really believe that anybody here except you has enough time
to collect all the pieces you cared to post scattered across many
messages and then try to make sense of them? Either debug your code
(using all the suggestions already given) or rewrite it from scratch
(which sometimes seems easier than trying to find the mistake). Do
what many professionals do in similar situations: divide and conquer
your code. Figure out which part are truly working fine: make sure
that pre- and post-conditions are met by introducing assertions into
your code. Write unit tests and run them. IOW, begin developing
your program properly instead of trying to take it by storm.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #10
I know my problem is complex, and I do thank all those for your time.
actually did rewrite this program seveal times from scratch. This is
my best reslut so far. I thought I had a pretty good design for the
program but it is that one line that is realy confusing me. I can make
the program work as I would like but the reason why I am doing this way
is for expansion in the future. I never realy studdied programming and
there are gaps in my knowlege. Most of my learning comes from doing
projects like this. I have some ideas for trying to make this work the
way I want. I will put the project down for a while and try some of
the changes.

The concept behind my program is that I have a grid of space objects.
I want each space to be able potentially to hold a character but there
is only one character in the game and in one space.

If I do class space{
char ch;

I create a character in each space>

I decited to do char * ch;
Still I will do some research but for my problem I am not sure where to
look.

Again thanks for the help.

May 18 '06 #11
JoeC wrote:
If there is no player then it will display a different graphic here is
the rest of the code:

graphic& space::graphicOut(){

if(play){
cgr = new graphic(play->gOut());
The above line looks like leaking memory. Just a guess, I am not sure.
return *cgr;
}
if(seen){return *gr;}
else {return *grDefault;}
}
Don't worry, every thing else works. I have gotten this to work by
creating the graphic in this object. I did it like this:

graphic& space::graphicOut(){
//if(play){return play->gOut();} <-This is the way I want to do
the graphic
//but it crashes.
if(play){return *cgr;}
if(seen){return *gr;}
else {return *grDefault;}
}


If you really want us to help, you've gotta help us to help you. This
usually involves creating the minimal compilable program that reproduces
the problem.

Regards,
Ben
May 18 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
1
by: n_o_s_p_a__m | last post by:
I have a method call in my static constructor which initializes 2 variables. I need to know when the static constructor (after the first call) will be called. Does unloading the virtual directory...
9
by: Alex Vinokur | last post by:
Compiler Green Hills C++, Version 4.0.6 --- foo.cpp --- struct A { }; struct B { B() {}
11
by: Rimpinths | last post by:
I'm new at developing user controls in C#, and one thing I've noticed right off the bat is that the constructor gets called twice -- once at design time, once at run time. In short, I'm trying...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
6
by: =?Utf-8?B?TWF0dA==?= | last post by:
I'm having a problem with a static class constructor being called twice. I have the static class MasterTaskList which uses a BackgroundWorker to execute multiple methods on a separate thread. The...
2
by: Studlyami | last post by:
After i create an object can i call the constructor again to reinitialize that objects variables? I know i can create a function to do this, but i was wonder if i can just call the constructor again....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.