Card Exception Exception

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

Unhandled exception at 0x77a015de in Main.exe: 0xC0000005: read access violation location 0x00000004.

"Readpoint 0x00000004" makes me think of something like a null pointer exception. However, the map itself is a static map initialized in a .cpp file. Why not have a place when trying to make an insert?

Here is the class. This is the player class that manages the display identifiers for 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 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 someone help me understand what is going on here?

EDIT: The code 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));
+3
source share
2 answers

, :

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

:

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

(: [] - , . , , , , . , , , .)


: . , :

// 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;
}

Player::players(), .

+4

- ?

, ++: undefined , .

+2

All Articles