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. Problem using struct as std::map value type

Problem using struct as std::map value type

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 6 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.
  • M Offline
    M Offline
    Manfr3d
    wrote on last edited by
    #1

    Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.

    F S M S J 5 Replies Last reply
    0
    • M Manfr3d

      Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.

      F Offline
      F Offline
      Franck Paquier
      wrote on last edited by
      #2

      hello i am not sure you can put a struct into a map. if i were you i would change the struct into a class with a copy constructor regards

      1 Reply Last reply
      0
      • M Manfr3d

        Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        You can't use aggregate syntax (that's the curly bracket stuff) anywhere except a struct/array variable initialiser. However, you certainly can put a struct in a map. Try the code below - it declares and initialises an SRect variable which it then inserts into the map: SRect sRect = {iX, iY, iW, iH}; RectMap.insert(make_pair(iID, sRect));

        1 Reply Last reply
        0
        • M Manfr3d

          Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.

          M Offline
          M Offline
          Malli_S
          wrote on last edited by
          #4

          What is your map definition? is it

          map<int, SRect *> RectMap;

          or

          map<int, SRect> RectMap;

          In the first case, you'll have to insert the structure variable into the map as below :

          RectMap\[0\] = new SRect();
          

          or

          SRect s;
          RectMap[0] = &s;

          i.e. before accessing the variable from map, make sure you've inserted it. And as you're saying RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); gives compiler error, I'm pretty much sure that your map declaration is as :

          std::map<int, SRect \*> RectMap;
          

          Better this will help you more...

          [Delegates]      [Virtual Desktop]      [Tray Me !]
          -Malli...! :rose:****

          1 Reply Last reply
          0
          • M Manfr3d

            Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.

            S Offline
            S Offline
            Sarath C
            wrote on last edited by
            #5

            I've few confusions. It's possible to store structure as a value inside map. std::map<int,srect> mymap;

            Austrian_Programmer wrote:

            void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; }

            As per your code, you created the map as std::map<int,srect*> mymap; Probably you might be accessing an NULL or invalid pointer.

            -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

            My blog - Sharing My Thoughts

            M 1 Reply Last reply
            0
            • S Sarath C

              I've few confusions. It's possible to store structure as a value inside map. std::map<int,srect> mymap;

              Austrian_Programmer wrote:

              void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; }

              As per your code, you created the map as std::map<int,srect*> mymap; Probably you might be accessing an NULL or invalid pointer.

              -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

              My blog - Sharing My Thoughts

              M Offline
              M Offline
              Manfr3d
              wrote on last edited by
              #6

              I found out that if I just use the struct and not struct pointers as map value type it's working without making any problems. Anyway, I don't understand why. In the meantime I also found a few code examples using a struct or a class as map value type, but I didn't see any examples using struct pointers.

              S 1 Reply Last reply
              0
              • M Manfr3d

                I found out that if I just use the struct and not struct pointers as map value type it's working without making any problems. Anyway, I don't understand why. In the meantime I also found a few code examples using a struct or a class as map value type, but I didn't see any examples using struct pointers.

                S Offline
                S Offline
                Sarath C
                wrote on last edited by
                #7

                I asked you regarding structure pointer as value because in the function you've mentioned using -> operator instead of . (dot) operator to access the members.

                -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

                My blog - Sharing My Thoughts

                1 Reply Last reply
                0
                • M Manfr3d

                  Hello guys, I wanna make a map that contains an integer key and a struct as value type. struct SRect { int iX, iY, iW, iH; }; map RectMap; This piece of code compiles fine. However, if I want to insert a pair into the map a runtime error occurs. void MakeParamRect(int iID, int iX, int iY, int iW, int iH) { RectMap[iID]->iX = iX; RectMap[iID]->iY = iY; RectMap[iID]->iW = iW; RectMap[iID]->iH = iH; } I've also tried it using the insert() function. RectMap.insert(make_pair(iID, {iX, iY, iW, iH})); In this case the struct notation with the curly brackets seems to produce the error (compiler says that a semicolon is missing before the first {). How can I make this code work, or is a struct not a valid value type for a map (it is valid in my opinion)? Thanks for your help.

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #8

                  map RectMap; <- error typedef unsigned key_type; <- any type you want std::map<key_type, SRect> RectMap; <- OK SRect sr = {iX, iY, iW, iH}; <- should work RectMap[iID] = sr; OR RectMap[iID] = {iX, iY, iW, iH}; <- should work Only write something like RectMap[iID]->iX if you are storing pointers to type sRect in the map; which would be bad and stuped unless you have a really good reason for doing so. If you are storing pointers ( std::map<key_type, SRect*> ) in the map, then you will need to write a method to free the memory pointed to by all those elements before you allow the map to be destroyed. If you are storing the actual data instead ( std::map<key_type, SRect> ), then the clean up will be automatic when the map is destroyed. To recap: ... BAD ...

                  std::map<key_type, SRect*> RectMap; <- bad
                  RectMap[key] = new SRect(iX, iY, iW, iH); <- this is why it is bad
                  void cleanup_before_destruction() <- and this is why it is bad
                  {
                  std::map<key_type, SRect*>::iterator i;
                  for( i = RectMap.begin(); i != RectMap.end(); ++i )
                  delete *i;
                  }

                  ... GOOD ...

                  std::map<key_type, SRect> RectMap; <- good
                  SRect sr(iX, iY, iW, iH);
                  RectMap[key] = sr;

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  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