I have a txt file with league, teams and players which looks like this :
League: some league
Team: some team
some players with name and strength
League: some other league
Now i read in the data with my readin function
#include "ReadIn.h"
#include "Player.h"
#include <deque>
#include <fstream>
#include <string>
#include <sstream>
#include <memory>
#include <iostream>
std::string zeile,word1, word2, sname, lname;
std::deque<Team*> Teamvector;
int str, a = 0, b = 0 , c = 0;
using namespace std;
void readin_fillVector(std::deque<Team> &v, std::deque<League> & w, std::vector<Player> u) {
ifstream fin("C:\\Users\\david\\Documents\\Visual Studio 2013\\Projects\\Anstoss2014\\Datenbank\\test.txt");
//Einlesen der Zeilen starten
while (getline(fin, zeile))
{
fin >> word1;
//find out if this line contains the team name or the players
if (word1 == "Liga:"){
getline(fin, word2);
//deleting the empty space in front of the team name
word2.erase(0, 1);
w.push_back(League(word2, c));
c++;
}
else if (word1 == "Verein:"){
getline(fin, word2);
//deleting the empty space in front of the team name
word2.erase(0, 1);
v.push_back(Team(word2, a));
//League gets the new member ( the team which was read in the line before)
w.back().AddTeam(&v.back());
//Team gets an pointer to the league it plays in
v.back().SetLeague(&w.back());
a++;
}
else{
fin >> lname >> str;
Player* player = new Player();
player->setinformation_(b, word1, lname, str, &v.back());
u.push_back(*player);
v.back().AddPlayer(player);
//for (size_t f = 0; f < v.back().GetPlayerList().size(); f++)
//{
// v.back().GetPlayerList()[f]->getinformation_();
//}
b++;
}
}
}
This works out like it should, but i'm confused with the line
Player* player = new Player();
I read much about pointers and there was said that this player created by new() should be deleted somehow. So first question is :1. Is this right ?
But if i do this in my function, the player's information stored in the team's playervector is lost.
2.I have boost accessable, should i use something like boost::ptr_vector ?
3.If not, what else could i do to avoid memory leak ?
EDIT:
Another way i tried this was
Player player;
player.setinformation_(b, word1, lname, str, &v.back());
u.push_back(player);
v.back().AddPlayer(&player);
But this ended in the the players having no more information stored when the function returns the teamvector. The output is just cryptic then.