Lets say I have a class player, which has the member variables num_houses and a pointer temp1 of type house
class player{
private:
int num_houses;
house* temp1;
public:
player(){
num_houses = 0;
temp1 = new house[num_houses];
}
My question lies with my code when I try to resize or basically add/remove an element of type house to my array of houses. In my code I happen to be getting a segmentation fault. I'm confused and I am a student so any advice or criticism will help me dearly!
void player::add_house(house tkn){
house* temp = new house[num_houses];
for(int i = 0; i < num_houses; i++){
temp[i] = temp1[i];
}
num_houses++;
if(temp1 != NULL){
delete [] temp1;
}
temp1 = new house[num_houses];
for(int i = 0; i < num_houses-1; i++){
temp1[i] = temp[i];
}
temp1[num_houses-1] = tkn;
delete [] temp;
}