Help With Copy Constructor.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JoeC

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

  • JoeC

    #2
    Re: Help With Copy Constructor.

    I am calling the copy constructr like this:

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

    In case this helps

    Comment

    • Phlip

      #3
      Re: Help With Copy Constructor.

      JoeC wrote:
      [color=blue]
      > BYTE get(int n){return gdata[n];}[/color]

      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!!!


      Comment

      • Dennis Jones

        #4
        Re: Help With Copy Constructor.


        "JoeC" <enki034@yahoo. com> wrote in message
        news:1146439853 .661368.187720@ g10g2000cwb.goo glegroups.com.. .[color=blue]
        > 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.[/color]

        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


        Comment

        • JoeC

          #5
          Re: Help With Copy Constructor.

          OK that is a start I took out some consts because I was getting errors.
          Thanks.

          Comment

          • JoeC

            #6
            Re: Help With Copy Constructor.

            OK, that helped, but it seems like my graphics data is not getting
            coppied.

            Comment

            • JoeC

              #7
              Re: Help With Copy Constructor.

              Thanks now it looks like it is tarting to work.

              Comment

              • Ron Natalie

                #8
                Re: Help With Copy Constructor.

                JoeC wrote:[color=blue]
                > 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.[/color]

                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.

                Comment

                • JoeC

                  #9
                  Re: Help With Copy Constructor.

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

                  Comment

                  • Ron Natalie

                    #10
                    Re: Help With Copy Constructor.

                    JoeC wrote:[color=blue]
                    > 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);
                    >[/color]

                    bitmap.bmBits = &gdata[0];

                    Comment

                    • JoeC

                      #11
                      Re: Help With Copy Constructor.

                      Thanks, I will have to redesign my whole class but I will give that a
                      try.

                      Comment

                      • JoeC

                        #12
                        Re: Help With Copy Constructor.

                        I tried what you sugested the program still crashes. What am I doing
                        wrong?

                        #include<window s.h>
                        #include<vector >
                        #include<iostre am>
                        #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>gda ta;
                        HBITMAP hbitmap;
                        BITMAP bitmap;
                        HDC hdc, hdcmem;
                        void copy(const BYTE in[]);
                        BYTE get(int n)const {return gdata[n];}
                        vector<BYTE>vGe t() 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,co nst int, const int);
                        };

                        #endif


                        #include<window s.h>
                        #include<fstrea m>
                        #include<string >
                        #include<vector >

                        #include "graphic.h"

                        using namespace std;

                        void graphic::copy(c onst 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 = CreateBitmapInd irect(&bitmap);
                        }

                        graphic::graphi c(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 = CreateBitmapInd irect(&bitmap);
                        }

                        Comment

                        • Dennis Jones

                          #13
                          Re: Help With Copy Constructor.


                          "JoeC" <enki034@yahoo. com> wrote in message
                          news:1146766337 .494189.227390@ u72g2000cwu.goo glegroups.com.. .[color=blue]
                          > I tried what you sugested the program still crashes. What am I doing
                          > wrong?[/color]

                          You will need to be more specific -- "still crashes" doesn't tell us
                          anything. What is crashing and where?

                          - Dennis


                          Comment

                          • JoeC

                            #14
                            Re: Help With Copy Constructor.

                            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(playe r*, 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::graphicO ut(){
                            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.

                            Comment

                            • Dennis Jones

                              #15
                              Re: Help With Copy Constructor.


                              "JoeC" <enki034@yahoo. com> wrote in message
                              news:1146968129 .282597.300520@ v46g2000cwv.goo glegroups.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


                              Comment

                              Working...