mardi 18 octobre 2022

How to delete an array that is in a struct inside a struct c++

This is for an assignment for one of my classes and I am stuck, I have to use these required structs, those being:

struct Pokemon {
    int dex_num;
    string name;
    string type;
    int num_moves;
    string* moves;
};

struct Pokedex {
    string trainer;
    int num_pokemon;
    Pokemon* dex;
};

I was tasked to create an array of pokemon with the available information from a .txt file. I name the Pokedex struct "Pokedex data;" what I am stuck on is the erasing of said array

void delete_info(Pokedex &);

The function above this text is how I have to delete it, and I am confused I have tried

delete []data.dex;
data.dex = NULL;

I have tried to dereference it and I have tried

delete []dex;

delete []data; 

etc.

Every single one has led me into a seg fault, or just general bugs and declaration issues.

edit this is how I was supposed to allocate the memory

Pokemon * dex  =  create_pokemons(7);

this is what I called for in my main


Pokemon* create_pokemons(int y) {
    Pokemon* dex = new Pokemon[y];
    return dex;
} 

i'm not quite sure what went wrong.

edit I am not allowed to use vectors

Aucun commentaire:

Enregistrer un commentaire