Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Still Struggling with map::insert and operator overload

Still Struggling with map::insert and operator overload

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingvisual-studioquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi I am still having problems with map::insert Here is my data

    struct usingx
    {
    unsigned char rectype;
    #define using 0
    #define pop 0x20
    #define push 0x40
    #define drop 0x80
    unsigned char usingflag
    #define oridinaryusing 0
    #define labeledusing 0x10
    #define depedentusing 0x20
    #define labeldependentusing 0x30
    ; ESDID locesdid;
    BYTE statement[4];
    BYTE location[4];
    BYTE usingvalue[4];
    BYTE laststatement[4];
    ESDID usingesdid;
    unsigned char registerx;
    BYTE displacement[2];
    BYTE reservedz;
    BYTE usingrange[4];
    BYTE reservedu[2];
    BYTE labeloffset[4];
    BYTE labelength[4];
    char* labelx;

    };

    Here is the key that I am using the start and end variable are big endian so I have to convert it and overloaded the = operator;

    struct usingrange
    {
    ESDID esdid;
    int start;
    int end;
    int operator=(BYTE z[4]) { return _byteswap_ulong((int)z[4]); };

    bool operator<(const usingrange x) const {
    if (esdid < x.esdid) return esdid < x.esdid;
    if (start < x.start) return start < x.start; if (end < x.end) return end < x.end;
    };

    here is the code using the map::insert

    	usingrange using\_range{ usingpoint->usingesdid };
    	using\_range.start = \*usingpoint->statement;
        using\_range.end = \*usingpoint->laststatement;
    	
    	procpointer->usingptr->insert({ using\_range,\*usingpoint });
    	break;
    

    I made breakpoints on the operator= code and thought when I stepped into it with the vs debugger it would trace it but it didnt both start and end ended up zeros dont know why ?

    M 1 Reply Last reply
    0
    • F ForNow

      Hi I am still having problems with map::insert Here is my data

      struct usingx
      {
      unsigned char rectype;
      #define using 0
      #define pop 0x20
      #define push 0x40
      #define drop 0x80
      unsigned char usingflag
      #define oridinaryusing 0
      #define labeledusing 0x10
      #define depedentusing 0x20
      #define labeldependentusing 0x30
      ; ESDID locesdid;
      BYTE statement[4];
      BYTE location[4];
      BYTE usingvalue[4];
      BYTE laststatement[4];
      ESDID usingesdid;
      unsigned char registerx;
      BYTE displacement[2];
      BYTE reservedz;
      BYTE usingrange[4];
      BYTE reservedu[2];
      BYTE labeloffset[4];
      BYTE labelength[4];
      char* labelx;

      };

      Here is the key that I am using the start and end variable are big endian so I have to convert it and overloaded the = operator;

      struct usingrange
      {
      ESDID esdid;
      int start;
      int end;
      int operator=(BYTE z[4]) { return _byteswap_ulong((int)z[4]); };

      bool operator<(const usingrange x) const {
      if (esdid < x.esdid) return esdid < x.esdid;
      if (start < x.start) return start < x.start; if (end < x.end) return end < x.end;
      };

      here is the code using the map::insert

      	usingrange using\_range{ usingpoint->usingesdid };
      	using\_range.start = \*usingpoint->statement;
          using\_range.end = \*usingpoint->laststatement;
      	
      	procpointer->usingptr->insert({ using\_range,\*usingpoint });
      	break;
      

      I made breakpoints on the operator= code and thought when I stepped into it with the vs debugger it would trace it but it didnt both start and end ended up zeros dont know why ?

      M Offline
      M Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      The line

      usingrange using_range{ usingpoint->usingesdid };

      does not invoke your assignment operator. This is a constructor and it will just copy the argument. Not sure where you expected the assignment to be invoked. I would replace the assignments with a constructor similar to this:

      struct usingrange
      {
      usingrange(usingx& x) {
      esdid = byte_swap(x.usingesdid;
      start = x.statement;
      end = x.laststatement;
      //…
      }
      ESDID esdid;
      int start;
      int end;

      As a comment, your comparison operator is suboptimal and incomplete. I’m surprised you didn’t get a warning that not all control paths return a value.

      Mircea

      F 1 Reply Last reply
      0
      • M Mircea Neacsu

        The line

        usingrange using_range{ usingpoint->usingesdid };

        does not invoke your assignment operator. This is a constructor and it will just copy the argument. Not sure where you expected the assignment to be invoked. I would replace the assignments with a constructor similar to this:

        struct usingrange
        {
        usingrange(usingx& x) {
        esdid = byte_swap(x.usingesdid;
        start = x.statement;
        end = x.laststatement;
        //…
        }
        ESDID esdid;
        int start;
        int end;

        As a comment, your comparison operator is suboptimal and incomplete. I’m surprised you didn’t get a warning that not all control paths return a value.

        Mircea

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        I understand at this point I am trying to get the =operator overload working I think it’s only referencing the first byte as opposed to 4 bytes Thought if I cast it it would work but didn’t going to try to move it to a int

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups