Storing data/editting vectors/misc

Hello! I'm fairly new to C++ (been teaching myself) and whilst I am confident enough using what I have learnt so far to make programs, sometimes I am unsure as to whether the way I am doing things is the best way, or whether there is something i need to learn that will enable me to do it much better. Anyway, I have two questions for you that are related, about the potential use of vectors.

So, say you were making a sports game, with hundreds of in game players. Each player has a name and some various attributes such as speed, stamina etc.

First question:
What would be the best/easiest/most professional way to store all this information?
My only idea so far would be to create a .txt file with all the data in and load this data into arrays/vectors/variables when the game starts up. This could also involve making a class of players, each with their name, attributes etc. Then a team class could be created so each team has a vector of player objects etc. With this set up it would be easy enough to create all the players/teams, and to be able to add new players. However I'm not so sure how to remove a specific player, which leads to my next question...

Second question:
How would you remove a specific element from a vector (or is there something easier to use than a vector for this purpose)?
Also if you had a long vector of players, but you didn't know the vector index of the player you wanted to remove, is it possible to remove it using a different reference, eg. removing a player by its name, or by an ID number associated with it?

Apologies if this is all a bit vague, but any help would be a great help!
To answer your first question:

Like you thought, the best way to store the information is in a file. However, I would not suggest a text file as this is easy to change/screw up by users and it may be annoying to parse when you are loading it. Instead lookup "Binary files" in the link below. This will let you store your classes and all existing information in them.
http://www.cplusplus.com/doc/tutorial/files/

To answer your second question:

Instead of having a vector of players, try creating a vector of pointers. That is, address to each player. Each player would be an instance of a class. To add and remove players from the field, you'd need to use the new and delete commands.

I asked a related question a few weeks ago which may help you: http://cplusplus.com/forum/general/55926/

Thanks for your help! I've looked up binary files and i can get them to work... mostly. There is a problem with strings, which means having to convert strings to cstrings, but thanks to the internet (its a wonderful thing!) I think i can manage that with some practice.

I am struglling with this vector of pointers thing though. Basically i have a player class with a constructor that takes a name and an array of attributes:

Player(std::string name, int attributes[18]);

The team class has a constuctor which takes a team name, size of the team, an array of the players names, and a 2d array of all the players attributes:

Team(std::string name, int teamsize, std::string playernames[], int playerattr[][18]);

In the team class i have my vector of pointers to player objects:

std::vector<Player*> players;


This is the team constructor which seems to be causing the problem (linker error, unresolved externals??):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Team::Team(std::string n, int t, std::string pn[], int pa[][18]) {
	name = n;
	teamsize = t;
	score = 0;
	for (int s = 0; s < 19; s++) {
		stats[s] = 0;
	}
	int tempatts[18];
        // loop over players
	for (int i = 0; i < t; i++) {
                // loop over attributes
		for (int j = 0; j < 18; j++) {
			tempatts[j] = pa[i][j];
		}
		Player *pointer;
		pointer = new Player(pn[i], tempatts);
		players.push_back(pointer);
	}
}


lines 15-17 is where i'm not sure about. I'm trying to create a new player object every iteration of i, then i need to make a pointer to that object and place it into the players vector. Is that the right idea? does new create a new object or just allocate the space for one? any help?
If you are getting a linker error this means that your code is fine (or at least it compiles). The problem that you are getting occurs after the code has been compiled and your IDE is trying to link everything together.

It means that you are referring to an object that is not available or accessible. A few common reasons that I experience are:
1. An object (Player?) is declared in a header but never defined (or you are missing the cpp file it belongs to).
2. You are using an external library (.lib) but you've forgotten to link it to the project.
Hi, I have finally managed to fix my problem, still not sure what was wrong with it though. I copied all my classes/main into a new project small bits at a time to check which bits worked and which bits caused the problems... then when it had all been copied... no link errors! working completely fine (so far!). I've even gone back to my old project and copied and pasted everything to make sure the 2 projects are identical, checked all the files were accounted for in both, yet still the old one has this link error, but the new one has no errors. This is why sometimes i hate computers!!!

Anyway thanks for your help Stewbound!
If the code was fine, then the problem was in your project settings. Sometimes I forget to link a library in both RELEASE and DEBUG compile modes. Or even worse, I include the debug/release library in a release/debug build. It makes it quite difficult to track on occasion.
Topic archived. No new replies allowed.