samedi 16 juin 2018

return Struct Pointer in function from a second struct

I have some problems with my class, there are two structs, the first struct (box_list) is like a parent and has pointers on a chain of single_box structs with values. I need a function that search for a value and returns the position of the single_box that contains the value. Maybe someone has an idea?

template <typename Num_v>
class Cont_Class{
    struct single_box { 
        int i; //value inside the single_box
        single_box *p;//pointer on another single_box
    };
    struct box_list{ 
        single_box *start; //pointer on the first single_box
        box_list():start(nullptr){}; //starts with a nullpointer, create later new single_boxes
  };
  box_list *table{nullptr};
  box_list *pos_p(const int &i) const; //should return a pointer on a single_box

  typename Cont_Class<Num_v>::box_list *Cont_Class<Num_v>::pos_p(const int &i) const {
      single_box *p2 = table[0].start; //position 0 is just an example
      while(p2){
        if (p2->i==i) return ;
        p2 = p2->p;}
      return 0;
  }
};

Aucun commentaire:

Enregistrer un commentaire