473,662 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help With Copy Constructor.

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\M y Documents\C++\D ungeon
Adventure2\gaph ic.cpp passing `const graphic' as `this' argument of
`BYTE graphic::get(in t)' discards qualifiers

C:\Documents and Settings\Work\M y Documents\C++\D ungeon
Adventure2\Make file.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::graphi c(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 = CreateBitmapInd irect(&bitmap);
}

Apr 30 '06 #1
27 3376
I am calling the copy constructr like this:

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

In case this helps

Apr 30 '06 #2
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!!!
Apr 30 '06 #3

"JoeC" <en*****@yahoo. com> wrote in message
news:11******** **************@ g10g2000cwb.goo glegroups.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::graphi c(const graphic& gr)
{
memcpy( gdata, gr.gdata, sizeof(BYTE) * sizeof(gdata) );
}

- Dennis
Apr 30 '06 #4
OK that is a start I took out some consts because I was getting errors.
Thanks.

Apr 30 '06 #5
OK, that helped, but it seems like my graphics data is not getting
coppied.

Apr 30 '06 #6
Thanks now it looks like it is tarting to work.

May 1 '06 #7
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\M y Documents\C++\D ungeon
Adventure2\gaph ic.cpp passing `const graphic' as `this' argument of
`BYTE graphic::get(in t)' discards qualifiers

C:\Documents and Settings\Work\M y Documents\C++\D ungeon
Adventure2\Make file.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::graphi c(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.
May 1 '06 #8
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 = CreateBitmapInd irect(&bitmap);

May 1 '06 #9
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 = CreateBitmapInd irect(&bitmap);


bitmap.bmBits = &gdata[0];

May 1 '06 #10

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

Similar topics

42
5758
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
15
21186
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 pointers) and for objects types call their default constructor. Any others points i should know?
2
2032
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" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
7
2383
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 me. This was inspired by Exercise 7 and Programming Problem 8 in Chapter 3 of our text. I have done Exercise 7 for you: Below you will find the ADT specification for a string of characters. It represents slightly more that a minimal string...
4
1788
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 fuzzy on copy. Could someone help me out with this class? Shown are the variables, constructors and destructor only (rest snipped out to preserve space). Yes, I realize this is fairly ugly code, any suggestions/critiques on that would be...
8
4292
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) {}
5
2004
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 //stack size private int stackTop; //variable to point to the top //of the stack private DataElement list; //array of reference variables
8
2000
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 ---------------- Original Copy-----------------------
0
920
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:
3
2305
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 I was understanding the chapter on policy classes was to attempt to apply them to my project. I understand the general concept of policies but I lack the knowledge and wisdom of how to identify them in an existing project. So I figured to get an...
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.