Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:20:03+00:00 2026-06-07T02:20:03+00:00

This one is gonna sting a little bit :\ . This is my map

  • 0

This one is gonna sting a little bit :\ . This is my map declaration:

std::map<std::string, std::shared_ptr<std::vector<std::shared_ptr<BaseEntity> > > > m_EntityByClassList;

really, the only reason it’s so long is because of the smart pointer declarations, it’s pretty much just this:

std::map<std::string, std::vector<BaseEntity *> *> m_EntityByClassList;

still painful to read though. here’s my problem. When i try to insert an object into a new vector in the map, i get an “access violation trying to read” runtime error.

the chunk of code that yields this error is this,

for(;;)
    {
        file >> classname;
        if(file.eof())
            break;
        m_EntityList.push_back( m_factory.createInstance(classname));
        m_EntityList.back()->Initialize();
        m_EntityList.back()->GetParams(file);
        m_EntityByNameList[m_EntityList.back()->GetName()] = m_EntityList.back();
        m_EntityByClassList[classname]->push_back(m_EntityList.back());
    }

now yes, there is certaintly alot of code i am omitting here, mainly because theres a good 35 or 36 objects and files that go into this blurb here so i’m just going to list off the things relevant. file is an std::ifstream to an open file, classname is an std::string, m_EntityList is a std::vector<std::shared_ptr<BaseEntity> > (shouldn’t matter), m_EntityByNameList is a map with indices type std::string and objects type std::shared_ptr<BaseEntity> (also shouldn’t matter) and then the map above. all the statements above the last one work exactly correct with verfied results. If the last line is omitted, it compiles and works just dandy.

So this question has nothing to do with the rest of the files, and is only about trying to add a vector to a map and add objects to it. If you feel you need anything else, ask and i’ll see about getting it posted up here. Posting everything would easily be 4000-4500 lines.

Annnnyway, what i’ve learned about maps from this is that using the [ ] operator with an indice that has yet to exist will have one created for you along with a default constructed object at that index. So what i get from this is that an std::shared_ptr<std::vector<std::shared_ptr<BaseEntity> > > will be created at the index classname which means i should be able to access the vector whether it had been created beforehand or just before. So does anyone have any clue what’s up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:20:05+00:00Added an answer on June 7, 2026 at 2:20 am

    Using operator[] on the map with a nonexistent key will indeed insert a new shared_ptr. However, what you aren’t taking into account is that the new shared_ptr will be empty (i.e. a null pointer), and therefore dereferencing it will cause undefined behavior. A new vector will not be created – since you are using pointers to vectors, you’ll need to allocate a new vector yourself.

    If your map’s value type was the vector itself rather than a pointer to it, it would be created automatically as you expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one is a little tricky. Say I have this XmlDocument <Object> <Property1>1</Property1> <Property2>2</Property2>
This one is a little confusing to me. I'm running a LAMP server with
I'm gonna go C++ über n00b on this one and ask how is the
Howdy fellas, this one is gonna be a doozy: So for awhile I've been
I have one base class which holds a map for function pointers like this
should be an easy one. I originally was gonna do this in javascript but
OK ... this is gonna be a long one. I have a iPhone 4
This one's a head scratcher. I've created a commented jsFiddle to demonstrate the phenomenon
This one's got me stumped and it's driving me nuts. I have a SiteFinity
This one has me pretty rattled so I thank you in advance for your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.