Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with std::map management
Forum Updated to NodeBB v4.3 + New Features

Problem with std::map management

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 881 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.
  • B BushyAxis793

    Hi. I have a problem managing access to the map. In my project, I added a file:

    HeaderVariablesContent.h
    HeaderVariablesContent.cpp

    In header file I added:

    extern std::map<EAutoCADVersionNumber, std::string> oAutoCADVersionNumberMap;
    

    In cpp:

    std::map<EAutoCADVersionNumber, std::string> oAutoCADVersionNumberMap = {
    
        {  EAutoCADVersionNumber::eR10,                   oAutoCADVersionNumberVector[0]},
        {  EAutoCADVersionNumber::eR11,                   oAutoCADVersionNumberVector[1]},
        {  EAutoCADVersionNumber::eR12,                   oAutoCADVersionNumberVector[1]},
        {  EAutoCADVersionNumber::eR13,                    oAutoCADVersionNumberVector[2]},
        {  EAutoCADVersionNumber::eR14,                    oAutoCADVersionNumberVector[3]},
        {  EAutoCADVersionNumber::eAutoCAD2000, oAutoCADVersionNumberVector[4]},
        {  EAutoCADVersionNumber::eAutoCAD2004, oAutoCADVersionNumberVector[5]},
        {  EAutoCADVersionNumber::eAutoCAD2007, oAutoCADVersionNumberVector[6]},
        {  EAutoCADVersionNumber::eAutoCAD2010, oAutoCADVersionNumberVector[7]},
        {  EAutoCADVersionNumber::eAutoCAD2013, oAutoCADVersionNumberVector[8]},
        {  EAutoCADVersionNumber::eAutoCAD2018, oAutoCADVersionNumberVector[9]},
        };
    

    The key and value of the map are added in HeaderVariablesDef.h

    extern std::vector<std::string> oAutoCADVersionNumberVector;
    
    enum class EAutoCADVersionNumber{
        eR10,
        eR11,
        eR12,
        eR13,
        eR14,
        eAutoCAD2000,
        eAutoCAD2004,
        eAutoCAD2007,
        eAutoCAD2010,
        eAutoCAD2013,
        eAutoCAD2018,
    };
    

    And HeaderVariablesDef.cpp

    std::vector<std::string> oAutoCADVersionNumberVector = {
        "AC1006",
        "AC1009",
        "AC1012",
        "AC1014",
        "AC1015",
        "AC1018",
        "AC1021",
        "AC1024",
        "AC1027",
        "AC1032"
    };
    

    In main.cpp I want to display map element but I got the error:

    libc++abi: terminating due to uncaught exception of type std::out_of_range: vector
    

    I'm not sure what it comes from. I think the code is written correctly. If anyone has any suggestions, I will be grateful.

    Have a good day
    BushyAxis793

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #3

    @BushyAxis793
    Run under debugger and see where the "out of range" comes from. Somewhere you are trying to access an element of a vector where the index is either below 0 or greater than [Edited to keep @SamiV123 happy: OR EQUAL TO!] the item count.

    S 1 Reply Last reply
    3
    • JonBJ JonB

      @BushyAxis793
      Run under debugger and see where the "out of range" comes from. Somewhere you are trying to access an element of a vector where the index is either below 0 or greater than [Edited to keep @SamiV123 happy: OR EQUAL TO!] the item count.

      S Offline
      S Offline
      SamiV123
      wrote on last edited by
      #4

      @JonB said in Problem with std::map management:

      @BushyAxis793
      Run under debugger and see where the "out of range" comes from. Somewhere you are trying to access an element of a vector where the index is either below 0 or greater than the item count.

      Except that std::vector's index type is unsigned so you can't access below 0 and accessing the index at item count is already undefined.

      JonBJ 1 Reply Last reply
      2
      • S SamiV123

        @JonB said in Problem with std::map management:

        @BushyAxis793
        Run under debugger and see where the "out of range" comes from. Somewhere you are trying to access an element of a vector where the index is either below 0 or greater than the item count.

        Except that std::vector's index type is unsigned so you can't access below 0 and accessing the index at item count is already undefined.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #5

        @SamiV123

        accessing the index at item count is already undefined.

        Meaning what? It will give std::out_of_range: vector. Which is what the user sees.

        I don't see why we disagreeing here. In general the way to see where such an error occurs is under a debugger.

        Yes, if there is no other code it may well be that oAutoCADVersionNumberMap = { is evaluated/executed before oAutoCADVersionNumberVector = { and that is the cause of the error. What harm does it do to check this under debugger?

        S 1 Reply Last reply
        1
        • S SamiV123

          @BushyAxis793

          Are those global objects? The construction order is undefined.

          B Offline
          B Offline
          BushyAxis793
          wrote on last edited by
          #6

          @SamiV123 Yes, global.

          S 1 Reply Last reply
          0
          • JonBJ JonB

            @SamiV123

            accessing the index at item count is already undefined.

            Meaning what? It will give std::out_of_range: vector. Which is what the user sees.

            I don't see why we disagreeing here. In general the way to see where such an error occurs is under a debugger.

            Yes, if there is no other code it may well be that oAutoCADVersionNumberMap = { is evaluated/executed before oAutoCADVersionNumberVector = { and that is the cause of the error. What harm does it do to check this under debugger?

            S Offline
            S Offline
            SamiV123
            wrote on last edited by SamiV123
            #7

            @JonB said in Problem with std::map management:

            @SamiV123

            accessing the index at item count is already undefined.

            Meaning what? It will give std::out_of_range: vector. Which is what the user sees.

            I don't see why we disagreeing here. In general the way to see where such an error occurs is under a debugger.

            Yes, if there is no other code it may well be that oAutoCADVersionNumberMap = { is evaluated/executed before oAutoCADVersionNumberVector = { and that is the cause of the error. What harm does it do to check this under debugger?

            actually the operator [] isn't required to be "safe".

            so therefore if you have

            std::vector<int> foo; foo[0];
            

            It's undefined behavior.

            I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

            JonBJ 1 Reply Last reply
            2
            • B Offline
              B Offline
              BushyAxis793
              wrote on last edited by
              #8

              Gentlemen, I found a mistake. One of the vectors in another file had 204 instead of 205 elements. Sorry for the confusion. And thank you for your help!

              1 Reply Last reply
              0
              • B BushyAxis793

                @SamiV123 Yes, global.

                S Offline
                S Offline
                SamiV123
                wrote on last edited by
                #9

                @BushyAxis793 said in Problem with std::map management:

                @SamiV123 Yes, global.

                Yeah, don't do that.

                either make a function like this

                std::vector<int> MyVec() {
                   static std::vector<int> foo {... };
                  return foo;
                }
                

                or use global pointers and restructure your code not to rely on global object initialization.

                1 Reply Last reply
                2
                • B BushyAxis793 has marked this topic as solved on
                • S SamiV123

                  @JonB said in Problem with std::map management:

                  @SamiV123

                  accessing the index at item count is already undefined.

                  Meaning what? It will give std::out_of_range: vector. Which is what the user sees.

                  I don't see why we disagreeing here. In general the way to see where such an error occurs is under a debugger.

                  Yes, if there is no other code it may well be that oAutoCADVersionNumberMap = { is evaluated/executed before oAutoCADVersionNumberVector = { and that is the cause of the error. What harm does it do to check this under debugger?

                  actually the operator [] isn't required to be "safe".

                  so therefore if you have

                  std::vector<int> foo; foo[0];
                  

                  It's undefined behavior.

                  I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #10

                  @SamiV123 said in Problem with std::map management:

                  I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

                  I don't know what you are on about or why you just seem to want to argue. Of course everyone knows "the last valid index is size - 1". My comment was not incorrect, I said just that. Are you quibbling over the fact that I wrote "or greater than the item count." when I could have written "greater than or equal to the item count." but didn't because I was trying to give the idea typing quickly, is that it? It doesn't matter whether you try to access [10] or [1000000] in a vector which has 10 elements, either one is "out of range". Why don't you actually try it and see whether you get uncaught exception of type std::out_of_range: vector instead of arguing semantics?

                  OP writes

                  One of the vectors in another file had 204 instead of 205 elements.

                  Oh look, just what I said: trying to access an element one (or could have been more, but 1 here) beyond the number in a vector....

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @SamiV123 said in Problem with std::map management:

                    I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

                    I don't know what you are on about or why you just seem to want to argue. Of course everyone knows "the last valid index is size - 1". My comment was not incorrect, I said just that. Are you quibbling over the fact that I wrote "or greater than the item count." when I could have written "greater than or equal to the item count." but didn't because I was trying to give the idea typing quickly, is that it? It doesn't matter whether you try to access [10] or [1000000] in a vector which has 10 elements, either one is "out of range". Why don't you actually try it and see whether you get uncaught exception of type std::out_of_range: vector instead of arguing semantics?

                    OP writes

                    One of the vectors in another file had 204 instead of 205 elements.

                    Oh look, just what I said: trying to access an element one (or could have been more, but 1 here) beyond the number in a vector....

                    S Offline
                    S Offline
                    SamiV123
                    wrote on last edited by SamiV123
                    #11

                    @JonB said in Problem with std::map management:

                    @SamiV123 said in Problem with std::map management:

                    I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

                    I don't know what you are on about or why you just seem to want to argue. Of course everyone knows "the last valid index is size - 1". My comment was not incorrect, I said just that. Are you quibbling over the fact that I wrote "or greater than the item count." when I could have written "greater than or equal to the item count." but didn't because I was trying to give the idea typing quickly, is that it? It doesn't matter whether you try to access [10] or [1000000] in a vector which has 10 elements, either one is "out of range". Why don't you actually try it and see whether you get uncaught exception of type std::out_of_range: vector instead of arguing semantics?

                    OP writes

                    One of the vectors in another file had 204 instead of 205 elements.

                    Oh look, just what I said: trying to access an element one (or could have been more, but 1 here) beyond the number in a vector....

                    I don't think everyone knows. It seems obvious that the OP is a newbie since if they weren't they would not have even posted this question.

                    So therefore they might have gone off the rails thinking that accessing the element at an off by one index is actually safe.

                    This is C++, you can't be "99%" correct you have to be 100% correct or your program is ill-defined. (which it always is anyway)

                    Thats all, peace out.

                    JonBJ Christian EhrlicherC 2 Replies Last reply
                    0
                    • S SamiV123

                      @JonB said in Problem with std::map management:

                      @SamiV123 said in Problem with std::map management:

                      I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

                      I don't know what you are on about or why you just seem to want to argue. Of course everyone knows "the last valid index is size - 1". My comment was not incorrect, I said just that. Are you quibbling over the fact that I wrote "or greater than the item count." when I could have written "greater than or equal to the item count." but didn't because I was trying to give the idea typing quickly, is that it? It doesn't matter whether you try to access [10] or [1000000] in a vector which has 10 elements, either one is "out of range". Why don't you actually try it and see whether you get uncaught exception of type std::out_of_range: vector instead of arguing semantics?

                      OP writes

                      One of the vectors in another file had 204 instead of 205 elements.

                      Oh look, just what I said: trying to access an element one (or could have been more, but 1 here) beyond the number in a vector....

                      I don't think everyone knows. It seems obvious that the OP is a newbie since if they weren't they would not have even posted this question.

                      So therefore they might have gone off the rails thinking that accessing the element at an off by one index is actually safe.

                      This is C++, you can't be "99%" correct you have to be 100% correct or your program is ill-defined. (which it always is anyway)

                      Thats all, peace out.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #12

                      @SamiV123 said in Problem with std::map management:

                      This is C++, you can't be "99%" correct you have to be 100% correct or your program is ill-defined. (which it always is anyway)

                      I will hold you to this in all your future posts, then ;-) [ <-- That's a wink! ]

                      For the record, I have edited my original post to be "100%" clear :)

                      1 Reply Last reply
                      1
                      • S SamiV123

                        @JonB said in Problem with std::map management:

                        @SamiV123 said in Problem with std::map management:

                        I was just pointing out that you can't access an element at the index that equals the size of the vector since the last valid index is size - 1 so your comment was incorrect.

                        I don't know what you are on about or why you just seem to want to argue. Of course everyone knows "the last valid index is size - 1". My comment was not incorrect, I said just that. Are you quibbling over the fact that I wrote "or greater than the item count." when I could have written "greater than or equal to the item count." but didn't because I was trying to give the idea typing quickly, is that it? It doesn't matter whether you try to access [10] or [1000000] in a vector which has 10 elements, either one is "out of range". Why don't you actually try it and see whether you get uncaught exception of type std::out_of_range: vector instead of arguing semantics?

                        OP writes

                        One of the vectors in another file had 204 instead of 205 elements.

                        Oh look, just what I said: trying to access an element one (or could have been more, but 1 here) beyond the number in a vector....

                        I don't think everyone knows. It seems obvious that the OP is a newbie since if they weren't they would not have even posted this question.

                        So therefore they might have gone off the rails thinking that accessing the element at an off by one index is actually safe.

                        This is C++, you can't be "99%" correct you have to be 100% correct or your program is ill-defined. (which it always is anyway)

                        Thats all, peace out.

                        Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @SamiV123 It's funny that you're arguing with 100% correct c++ here but advertising a proprietary way to link against libraries ('# pragma comment(lib, "ws2_32.lib")') in another thread.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        S 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @SamiV123 It's funny that you're arguing with 100% correct c++ here but advertising a proprietary way to link against libraries ('# pragma comment(lib, "ws2_32.lib")') in another thread.

                          S Offline
                          S Offline
                          SamiV123
                          wrote on last edited by SamiV123
                          #14

                          @Christian-Ehrlicher said in Problem with std::map management:

                          @SamiV123 It's funny that you're arguing with 100% correct c++ here but advertising a proprietary way to link against libraries ('# pragma comment(lib, "ws2_32.lib")') in another thread.

                          Yeah and that's completely orthogonal to this here.
                          Using a compiler specific way to link your program doesn't make it ill-defined (just possibly non portable).

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • S SamiV123

                            @Christian-Ehrlicher said in Problem with std::map management:

                            @SamiV123 It's funny that you're arguing with 100% correct c++ here but advertising a proprietary way to link against libraries ('# pragma comment(lib, "ws2_32.lib")') in another thread.

                            Yeah and that's completely orthogonal to this here.
                            Using a compiler specific way to link your program doesn't make it ill-defined (just possibly non portable).

                            Christian EhrlicherC Online
                            Christian EhrlicherC Online
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @SamiV123 said in Problem with std::map management:

                            just possibly non portable

                            Which contradicts the c++ idea.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            S 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @SamiV123 said in Problem with std::map management:

                              just possibly non portable

                              Which contradicts the c++ idea.

                              S Offline
                              S Offline
                              SamiV123
                              wrote on last edited by
                              #16

                              @Christian-Ehrlicher said in Problem with std::map management:

                              @SamiV123 said in Problem with std::map management:

                              just possibly non portable

                              Which contradicts the c++ idea.

                              Maybe so but that's not how real life works. In real life practically every C++ program is only as portable as the developers intended, i.e. it only compiles and runs on the platforms targeted by the developers.

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved