Reputation:
I am trying to write a soccer team register class (SocReg) in C++ which when the array of teams is full it will copy all the data in a new array with 10 more free spaces, destroy the existing one and then continue working. However it seems those are not saved in the array, only the new ones get to be registered and the old ones are copied as "" nothing.
Team * tempTeams = new Team[theSize+10];
for(int i = 0; i < theSize; i++){
teams[i] = tempTeams[i];
}
theSize += 10;
delete[] teams;
teams = tempTeams;
unused++;
teams[unused+1] = Team(teamName,color);
Here is my copy constructor in Team Class
Team::Team(Team& toCopy){
tName = toCopy.tName;
tCol = toCopy.tCol;
unused = toCopy.unused;
roster = new Player*[50];
for(int i = 0; i < 50; i++)
roster[i] = toCopy.roster[i];
}
Have done anything wrong here?
Upvotes: 0
Views: 147
Reputation: 248
You should change
for(int i = 0; i < theSize; i++){
teams[i] = tempTeams[i];
}
to
for(int i = 0; i < theSize; i++){
tempTeams[i] = teams[i];
}
because at the moment you're copying data in the wrong direction, effectively storing empty strings.
Upvotes: 3
Reputation: 18964
You're copying elements from tempTeams
to teams
; it should be the other way round.
std::vector
would do all of this automatically for you.
Upvotes: 2