I am still working on my game and my program is getting better. Most
of what I want to works. I think I am having trouble with copy
constructor. Basically I want it to copy the gdata array. My gdata
will work if I do: gdata[32] = this->get(lp); But that is not what I
want to do I get some cryptic errors when I write the code like that is
commented out.
49 C:\Documents and Settings\Work\My Documents\C++\Dungeon
Adventure2\gaphic.cpp passing `const graphic' as `this' argument of
`BYTE graphic::get(int)' discards qualifiers
C:\Documents and Settings\Work\My Documents\C++\Dungeon
Adventure2\Makefile.win [Build Error] [gaphic.o] Error 1
I am pretty good with pointer and objects but my compiler is fustrating
me. Can I get some advice to make this copy constructer do what it is
suppoded to do.
class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
BYTE gdata[32]; //bitmap array
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hdcmem;
void copy(BYTE in[]);
BYTE get(int n){return gdata[n];}
public:
graphic();
graphic(BYTE c[]);
graphic(const graphic&);
graphic& operator = (graphic&);
void SetGr(BYTE c[]);
void set(BYTE c[]);
void display(HWND, int, int);
};
graphic::graphic(const graphic& gr){
for(int lp = 0; lp != 32; lp++){
//gdata[32] = gr.get(lp); <- I get errors.
}
ud = lr = 16;
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gdata;
hbitmap = CreateBitmapIndirect(&bitmap);
} 27 3348
I am calling the copy constructr like this:
if(play){
cgr = new graphic(play->gOut());
return *cgr;
}
In case this helps
JoeC wrote: BYTE get(int n){return gdata[n];}
BYTE get(int n) const {return gdata[n];}
Now Google for "const correct". You should make constant any method that
doesn't change *this.
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com... I am still working on my game and my program is getting better. Most of what I want to works. I think I am having trouble with copy constructor. Basically I want it to copy the gdata array. My gdata will work if I do: gdata[32] = this->get(lp); But that is not what I want to do I get some cryptic errors when I write the code like that is commented out.
It seems to me that the easiest and most straight-forward way to copy the
contents of gdata is to simply do this:
graphic::graphic(const graphic& gr)
{
memcpy( gdata, gr.gdata, sizeof(BYTE) * sizeof(gdata) );
}
- Dennis
OK that is a start I took out some consts because I was getting errors.
Thanks.
OK, that helped, but it seems like my graphics data is not getting
coppied.
Thanks now it looks like it is tarting to work.
JoeC wrote: I am still working on my game and my program is getting better. Most of what I want to works. I think I am having trouble with copy constructor. Basically I want it to copy the gdata array. My gdata will work if I do: gdata[32] = this->get(lp); But that is not what I want to do I get some cryptic errors when I write the code like that is commented out.
49 C:\Documents and Settings\Work\My Documents\C++\Dungeon Adventure2\gaphic.cpp passing `const graphic' as `this' argument of `BYTE graphic::get(int)' discards qualifiers
C:\Documents and Settings\Work\My Documents\C++\Dungeon Adventure2\Makefile.win [Build Error] [gaphic.o] Error 1
I am pretty good with pointer and objects but my compiler is fustrating me. Can I get some advice to make this copy constructer do what it is suppoded to do.
class graphic{ int btmap; int lr,ud; //Diminsion (size) of the graphic BYTE gdata[32]; //bitmap array HBITMAP hbitmap; BITMAP bitmap; HDC hdc, hdcmem; void copy(BYTE in[]); BYTE get(int n){return gdata[n];}
public: graphic(); graphic(BYTE c[]); graphic(const graphic&); graphic& operator = (graphic&); void SetGr(BYTE c[]); void set(BYTE c[]); void display(HWND, int, int); };
graphic::graphic(const graphic& gr){ for(int lp = 0; lp != 32; lp++){ //gdata[32] = gr.get(lp); <- I get errors.
gdata[lp] = gr.get(lp);
I suspect you don't need the get() fucntion here, if all it returns is
gdata[lp] because gr.gdata while private is accessible here.
gdata[lp] = gr.gdata[lp];
You could avoid a lot of silliness by not using the busted
C++ array type and use vector which has proper copy semantics.
I assume eventually, you'll want to get rid of the assumptions
that ud and lr are both 16.
I would like to use a vector but all this dosn't seem to accept a
vector when I did some earlier experiments. I would much rather use a
standard libray.
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gdata; <- onc etried a vector here and it didn't
work.
hbitmap = CreateBitmapIndirect(&bitmap);
JoeC wrote: I would like to use a vector but all this dosn't seem to accept a vector when I did some earlier experiments. I would much rather use a standard libray.
BITMAP bitmap = {0,ud,lr,2,1,1}; bitmap.bmBits = gdata; <- onc etried a vector here and it didn't work. hbitmap = CreateBitmapIndirect(&bitmap);
bitmap.bmBits = &gdata[0];
Thanks, I will have to redesign my whole class but I will give that a
try.
I tried what you sugested the program still crashes. What am I doing
wrong?
#include<windows.h>
#include<vector>
#include<iostream>
#include<string>
using namespace std;
#ifndef GRAPHIC_H
#define GRAPHIC_H
class graphic{
int btmap;
int lr,ud; //Diminsion (size) of the graphic
vector<BYTE>gdata;
HBITMAP hbitmap;
BITMAP bitmap;
HDC hdc, hdcmem;
void copy(const BYTE in[]);
BYTE get(int n)const {return gdata[n];}
vector<BYTE>vGet() const {return gdata;}
public:
graphic();
graphic(const BYTE c[]);
graphic(const graphic&);
graphic& operator = (const graphic&);
void SetGr(const BYTE c[]);
void set(const BYTE c[]);
void display(HWND,const int, const int);
};
#endif
#include<windows.h>
#include<fstream>
#include<string>
#include<vector>
#include "graphic.h"
using namespace std;
void graphic::copy(const BYTE in[]){
for(int lp=0; lp != 32; lp++)
gdata.push_back(in[lp]);
}
void graphic::SetGr(const BYTE c[]){
//Changing the graphic
copy(c);
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}
graphic::graphic(const graphic& gr){
ud = lr = 16;
//memcpy( gdata, gr.gdata, 32 );
gdata = gr.vGet();
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}
"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com... I tried what you sugested the program still crashes. What am I doing wrong?
You will need to be more specific -- "still crashes" doesn't tell us
anything. What is crashing and where?
- Dennis
I am experimenting with different ways to do what I want. It is
tricky. I want the graphics data to be held in the objects on a grid
of space objects. That works. But what I want to do is to have
objects stored in those objects with graphic data and I want that that
data to be displayed on the screen. I got the program to run like I
would like but it the program is not written to be expanded. I want to
have variouse kinds of objects that contain graphics data later and I
don't want to keep updating my board object with new data. That is how
I made it work, I put the graphics data for my player on the map with
all the graphics for the map.
class board{
player * play;
static const int size = 30;
map<char, coord> keys;
space spaces[size][size];
coord n;
coord s;
coord e;
coord w;
ifstream& cfill(ifstream&, char&); /*Reads map from file */
void fill();
coord find();
void seeing(int, int);
public:
board();
void setPlayer(player*, int, int);
graphic& display(int, int);
int sze(){return size;}
void move(char);
};
There might or might not be a player in the map so I use a pointer. I
wand the graphics data from that player to be returned to the main
program for display.
graphic& space::graphicOut(){
if(play){
BYTE * cr = &play->GetData();
cgr = new graphic(cr);
//&play->GetData());
return *cgr; //I want to get the player's graphic data either the
bitmap array or
the graphic object and return it for display.
}
if(seen){return *gr;} //returns the graphic on the map (space or
wall)
else {return *grDefault;} //returns unseen space (blank)
}
This is the trick of what I am trying to do.
"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
<irrelevant content snipped>
I thought you wanted help with a crash, so I asked how and where your
program was crashing. You didn't answer that question and now you seem to
be talking about something else. Was there supposed to be a question
somewhere in that last message?
- Dennis
It is a complex problem I am working on. Still thanks for the help.
The best I can do right now is get the graphic to show garbled. So
some where I a loosing the graphics data. Thanks for your time.
I have been working on this problem for a while and still my program is
getting better. The object seems to work but it looks like my graphics
information is getting corrupted.
What I do is create the graphic
Then I change the graphics information
finlaay I use the copy constructor.
Here is the code:
graphic::graphic(){
lr = ud =16;
BYTE c[32] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
copy(c);
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gdata;
hbitmap = CreateBitmapIndirect(&bitmap);
}
void graphic::set(const BYTE c[]){
copy(c);
ud = lr = 16;
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gdata;
hbitmap = CreateBitmapIndirect(&bitmap);
}
graphic::graphic(const graphic& gr){
ud = lr = 16;
//memcpygdata, gr.gdata, 32 );
for(int lp = 0; lp != 32; lp++){
gdata[lp] = gr.get(lp);}
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = gdata;
hbitmap = CreateBitmapIndirect(&bitmap);
}
"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@g10g2000cwb.googlegr oups.com... I have been working on this problem for a while and still my program is getting better. The object seems to work but it looks like my graphics information is getting corrupted.
What I do is create the graphic Then I change the graphics information finlaay I use the copy constructor.
<snip>
If 'gdata' is a vector (which I think you stated in one of your previous
messages), you can simplify things significantly. If 'gdata' is not already
a vector, consider making it a vector so that you can do this (note the
simplicity -- no 'copy' loop):
graphic::graphic(const graphic& gr)
{
...
gdata = gr.gdata;
...
}
Okay, now let's refactor things a little. I think it would be good to
factor out the bitmap creation code into its own function, because it is
getting duplicated in several places:
HBITMAP graphic::createBitmap( void )
{
BITMAP bitmap = { 0, ud, lr, 2, 1, 1 };
bitmap.bmBits = &gdata[0];
HBITMAP hBitmap = CreateBitmapIndirect(&bitmap);
return hBitmap;
}
Now IMO, an "ideal" copy constructor would perform a _simple_ assignment of
as many data members as it possibly can, only performing more complex copy
operations if/when it is absolutely necessary. For instance, obviously, you
can't do a simple assignment with pointer members (such as hbitmap), but you
can with most other members (assuming they support an assignment operator,
like vector does). So, I think I would probably write the copy-constructor
like this:
graphic::graphic(const graphic& other)
{
ud = other.us;
lr = other.lr;
gdata = other.gdata;
hbitmap = createBitmap();
}
You don't need the "copy" function. Assuming you make 'gdata' a vector, a
simple assignment will work just fine (and it makes the code easier to read
and understand and more robust in the face of change).
I'm not sure why you have a "set" method, since it pretty much does the same
thing as the copy-constructor, but if you need to keep it for some reason, I
would change the 'c' argument to be a vector, like this:
void graphic::set( const std::vector<BYTE> &c )
{
gdata = c;
hbitmap = createBitmap();
}
And your default constructor becomes:
graphic::graphic()
{
ud = lr = 16;
for ( int i=0; i<32; ++i )
{
gdata.push_back( 0xFF );
}
hbitmap = createBitmap();
}
I hope this makes sense to you and provides you with some direction.
- Dennis
Thanks for the time in explaining how that works.
That helped, ithink my graphic object is much better but it still
crashes at this point:
graphic::graphic(const graphic& gr){
ud = lr = 16;
//gdata = gr.gdata; <-- This crashes
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}
That bity of code crashes when I use the command like this:
if(play){
cgr = new graphic(play->gOut());
return *cgr;
--------------
graphic gOut() {return gr;}
Thanks for the time in explaining how that works.
That helped, ithink my graphic object is much better but it still
crashes at this point:
graphic::graphic(const graphic& gr){
ud = lr = 16;
//gdata = gr.gdata; <-- This crashes
BITMAP bitmap = {0,ud,lr,2,1,1};
bitmap.bmBits = &gdata[0];
hbitmap = CreateBitmapIndirect(&bitmap);
}
That bity of code crashes when I use the command like this:
if(play){
cgr = new graphic(play->gOut());
return *cgr;
--------------
graphic gOut() {return gr;}
It works if I coment out that part but I get a block instead of my
graphic, I think I am getting close.
JoeC wrote: graphic::graphic(const graphic& gr){ ud = lr = 16;
assert(this != & gr);
//gdata = gr.gdata; <-- This crashes
Are you sure you are not assigning to yourself?
--
Phlip http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
"JoeC" <en*****@yahoo.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com... Thanks for the time in explaining how that works.
That helped, ithink my graphic object is much better but it still crashes at this point:
graphic::graphic(const graphic& gr){ ud = lr = 16; //gdata = gr.gdata; <-- This crashes BITMAP bitmap = {0,ud,lr,2,1,1}; bitmap.bmBits = &gdata[0]; hbitmap = CreateBitmapIndirect(&bitmap); }
That bity of code crashes when I use the command like this:
if(play){ cgr = new graphic(play->gOut()); return *cgr; -------------- graphic gOut() {return gr;}
It works if I coment out that part but I get a block instead of my graphic, I think I am getting close.
Is 'gdata' a vector?
- Dennis
"Phlip" <ph******@yahoo.com> wrote in message
news:ux******************@newssvr14.news.prodigy.c om... JoeC wrote:
graphic::graphic(const graphic& gr){ ud = lr = 16;
assert(this != & gr);
//gdata = gr.gdata; <-- This crashes
Are you sure you are not assigning to yourself?
I think you're onto something there, Phlip. I hadn't even considered that
possibility. Joe, if the 'assert' fails, then that's definitely your
problem. However, rather than asserting, it would be better to handle the
special case:
graphic::graphic(const graphic& gr)
{
if ( this == &gr ) return;
...
}
- Dennis
It is now I made the changes that were suggested. It does work better
if I take that statment out it just displays a block and don't copy the
graphic info. If I try to copy the program crashes.
"JoeC" <en*****@yahoo.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com... It is now I made the changes that were suggested. It does work better if I take that statment out it just displays a block and don't copy the graphic info. If I try to copy the program crashes.
Again, you'll need to provide more info. -- just saying "the program
crashes" isn't very helpful. Have you tried stepping through the code with
a debugger to watch the values of critical variables when it crashes? Maybe
you should ask in one of the Windows newsgroups -- this group is about C++
in general, not Windows.
- Dennis
Again thanks for the help. I thought this was more of a c++ problem.
If gdata = gr.gdata; copys the vector then it should work. I see no
reason why id dosn't. I don't need to copy each element or dop I need
to have a getter statment from gr object in the copy constructor.
"JoeC" <en*****@yahoo.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com... Again thanks for the help. I thought this was more of a c++ problem. If gdata = gr.gdata; copys the vector then it should work. I see no reason why id dosn't.
Exactly -- from a C++ perspective, it should work. If it doesn't, there is
something else wrong, perhaps in your use of Windows code.
I don't need to copy each element
I'm not exactly sure what you are saying here. If you are referring to the
vector elements, then no, you do not need to copy each element of the vector
because a single assignment copies the entire contents of the vector into
the new vector. However, if you are referring to the class members, then I
think it makes good sense to copy each of the members, even if you could
initialize them explicitly -- I think it makes the intent of the copy
constructor clearer (you're copying the contents of one object into another)
and it will probably lead to better C++ habits in the long run.
or dop I need to have a getter statment from gr object in the copy constructor.
Again, I'm not sure what you are asking. You do not need a getter method to
access data members of the same class -- private members are always
accessible directly.
- Dennis
If it should work, then I might have been OK on the C++ side. I have
learned quite a bit with project. I have read in theory about objects
and copying but it is another thing to do it in practice. I will ask
on another group. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Edward Diener |
last post by:
Coming from the C++ world I can not understand the reason why copy
constructors are not used in the .NET framework. A copy constructor creates
an object from a copy of another object of the same...
|
by: A |
last post by:
Hi,
A default copy constructor is created for you when you don't specify one
yourself. In such case, the default copy constructor will simply do a
bitwise copy for primitives (including...
|
by: dinks |
last post by:
Hi,
I'm new to C++ and have been assigned a task which i dont completely
understand. Any help would be greately appreciated.
Here is the problem:
The class "linkedListType" use the "assert"...
|
by: Alan Bashy |
last post by:
Please, guys, In need help with this. It is due in the next week. Please,
help me to implement the functions in this programm especially the first
three constructor. I need them guys. Please, help...
|
by: Jim Langston |
last post by:
I understand the rule of three, that if I have a custom constructor, copy or
destructor I probably need the other 2. My class object definately has a
custom constructor and destructor, but I'm...
|
by: shuisheng |
last post by:
Dear All,
I am wondering how the default copy constructor of a derived class
looks like. Does it look like
class B : public A
{
B(const B& right)
: A(right)
{}
|
by: satan |
last post by:
I need a help in my method equalStack in my class StackClass.
public class StackClass
{
private int maxStackSize; //variable to store the maximum
...
|
by: john |
last post by:
Hey guys,
Quick question i have this code and what i want to do is create a deep
copy of class B. Now I tried doing this with the new operator and
pointers. Here's is the orignal
...
|
by: bayan1 |
last post by:
The following program have the following output. write the necessary code to make it works?
Code :
#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
|
by: Stephen Torri |
last post by:
Below is a class that is suppose to represent a segment of memory or a
contents of a binary image (e.g. ELF executable). I have started to read
Modern C++ Design and thought the best way to ensure...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |