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 7929071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:57:06+00:00 2026-06-03T19:57:06+00:00

When I try to insert an object into my map, it throws the following

  • 0

When I try to insert an object into my map, it throws the following exception:

Unhandled exception at 0x77a015de in Main.exe: 0xC0000005: Access violation reading location 0x00000004.

The “reading location 0x00000004” makes me think it’s some sort of null pointer exception. However, the map itself is a static map, initialized in the .cpp file. Why wouldn’t it have a location when trying to do the insert?

Here’s the class. It’s a player class that manages mapping IDs to players:

// Player.hpp
class Player {
  public:

    Player(){}; // I had to make this public, otherwise it wouldn't compile.
    Player(Player &p);
    Player & operator=(const Player &p);
    bool operator==(const Player &p);

    int getID() const;
    int getTeam() const;
    string getName() const;
    Vec3 getColor() const;

    static Player newPlayer(int team, string name, Vec3 color);

  private:
    Player(int id, int team, string name, Vec3 color);
    int id,
        team;
    string name;
    Vec3 color;

    static std::map<int, Player> players;
};

and the cpp file:

#include "Player.hpp"
std::map<int, Player> Player :: players;
int Player :: currentPlayer=-1;

// Constructor
Player :: Player(Player &p) : id(p.getID()),
                              team(p.getTeam()),
                              name(p.getName()),
                              color(p.getColor()){}

Player & Player :: operator=(const Player &p){
  if (this==&p){
    return *this;
  }

  id=p.getID();
  team=p.getTeam();
  name=p.getName();
  color=p.getColor();

  return *this;
}

bool Player :: operator==(const Player &p){
  return p.getID()==getID();
}

// Factory
Player Player :: newPlayer(int team,
                           string name,
                           Vec3 color){
  int playerID=0;
  if (players.size()>0){
    int playerID=(*players.rbegin()).first+1; // get an ID higher than the largest already there.
  }

  Player p(playerID, team, name, color);
  players.insert(std::make_pair(playerID, p)); // EXCEPTION THROWN HERE
  return players[playerID];
}

// Internal Constructor
Player :: Player(int id,
                 int team,
                 string name,
                 Vec3 color) : id(id),
                               team(team),
                               name(name),
                               color(color){}

Can anyone help me understand what’s going on here?

EDIT:
The code that’s calling this method is in main.hpp, outside of any methods, in the global scope:

Player player1=Player::newPlayer(1, "p1", Vec3(0.2, 0.2, 0.8)),
       player2=Player::newPlayer(2, "p2", Vec3(0.8, 0.2, 0.2));
  • 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-03T19:57:09+00:00Added an answer on June 3, 2026 at 7:57 pm

    To address the need for a public default constructor, try changing this:

    Player p(playerID, team, name, color);
    players.insert(std::make_pair(playerID, p)); // EXCEPTION THROWN HERE
    return players[playerID];
    

    into this:

    return players.insert(std::make_pair(playerID, Player(playerID, team, name, color))).first->second;
    

    (Explanation: The []-operator requires that your mapped type be default-constructible. Using it means that you’re ignoring the knowledge that the key that you’re looking up exists in the map. The operator wants to create a new default-constructed element if the key that you give it doesn’t exist.)


    Update: You also have to address the static initialization problem. Here’s one way how to do this:

    // header:
    
    class Player {
    public:
        static std::map<int, Player> & players();
        // ...
    };
    

    // implementation:
    
    std::map<int, Player> & Player::players()
    {
        static std::map<int, Player> impl;
        return impl;
    }
    

    Now just use Player::players() to get a reference to the map.

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

Sidebar

Related Questions

I get an invalid object name error when I try to insert records into
I get the following error when I try to insert a row into a
I tried to insert an object into a generic BindingList. But if I try
I am getting an error when I try to insert an item into a
I have the following table. When i try to insert records using LINQ to
If I try this statement: INSERT INTO TerminalEventChild (id,stringValue) VALUES (64,'version123|'); MySQL fail with
I try to write the following in latex: \begin{itemize} \item \textbf{insert(element|text)} inserts the element
I'm trying to insert some value pairs into a std::map. In the first case,
I am trying to insert a date object into the database but it says
i was getting familiar with sqlite and try to insert the values into database

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.