Ok, hopefully this is going to me some sense to someone because I am losing lots of hair over it now.
I have an 'Application' class, with one instance defined as a global. This is basically a starting point to avoid having lots of globals.
The application is the server side of a multiplayer game, so I have another class called 'Player' that holds information about players. (of which there can be lots)
Inside the Application class, I define a list in which I create my player objects, one for each player.
I am trying to also use maps to create a sort of lookup table to quickly find players in the list based on a specific piece of information about the player. (in this case, username)
In the constructor of the Player object, I am trying to store a pointer to 'this' along with the players username in the map called _Player_Logins.
When doing this, I receive 'Segmentation Fault'.
I am really at a loss as to why I am getting this so I am hoping someone else might know.
Here is a very basic example of what I am trying to do. (code is tested)
main.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <string>
using namespace std;
class Player {
public:
string login;
string password;
Player();
};
class Application {
public:
list<Player *> _Players;
map<string, Player *> _Player_Logins;
Application();
};
int main();
|
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <list>
#include <map>
#include <iostream>
#include "main.h"
using namespace std;
Application *mApp;
Application::Application() {
_Players.push_back(new Player());
};
Player::Player() {
login="testLogin";
password="secret";
mApp->_Player_Logins[login]=this;
};
int main() {
mApp=new Application();
cout << mApp->_Player_Logins["testLogin"]->password << "\n";
return(0);
};
|
My sincere thanks to anyone who spends a little time looking at this.