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:
class Player {
public:
Player(){};
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;
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();
}
Player Player :: newPlayer(int team,
string name,
Vec3 color){
int playerID=0;
if (players.size()>0){
int playerID=(*players.rbegin()).first+1;
}
Player p(playerID, team, name, color);
players.insert(std::make_pair(playerID, p));
return players[playerID];
}
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));