1

Tengo estas estructuras

typedef struct stDC {
    qint8 numberA;
    qint16 numberB;
    qint32 numberC;
    qint64 numberD;
    qint32 *intList;
    qint8 *byteList;
    stDC *list;
    int listlen;
    QString string;
    int mode;
    float floatv;
    double doublev;
} DataComplex;

typedef struct stTab {
    QString name;
    DataComplex value;
} Tab;

typedef struct stTree {
    QString name;
    Tab data[255];
    int data_n;
    stTree *branches[255];
    int branches_num;
} Tree;

Pero necesito agregar un array de punteros Tree a DataComplex es esto posible?, y si lo es como?

1 respuesta 1

1

Es perfectamente posible; y, además, muy simple: pre-declarando:

struct stTree; // <- PRE-DECLARAMOS.

typedef struct stDC {
  qint8 numberA;
  qint16 numberB;
  qint32 numberC;
  qint64 numberD;
  qint32 *intList;
  qint8 *byteList;
  stDC *list;
  int listlen;
  QString string;
  int mode;
  float floatv;
  double doublev;
  stTree *que_facil_es[1024]; // <-- AQUÍ.
} DataComplex;

En la pre-declaración, informamos al compilador de que un identificador es una clase; así, cuando el compilador encuentre dicho identificador, sabe que lo vamos a declarar mas pronto o mas tarde, y no se queja.

Si te fijas detalladamente, verás que ya lo estas haciendo, incluso antes de formular la pregunta:

typedef struct stTree {
  QString name;
  Tab data[255];
  int data_n;
  stTree *branches[255]; // <-- ¿¿ Cómo es posible esto ??
};

¿ Como es posible que uses un arreglo de punteros a stTree antes de terminar de declarar la struct ?

Esta técnica solo es válida si vamos a usar punteros o referencias; por ejemplo:

struct stTree; // <- PRE-DECLARAMOS.

typedef struct stDC {
  qint8 numberA;
  ...
  stTree que_facil_es; // <-- ESTO ES UN ERROR.
} DataComplex;

Piensa que el compilador necesita conocer el tamaño de los tipos; en el código anterior, no lo sabe; por lo tanto, no sabe cuanta memoria ha de usar, y se queja.

Sin embargo, siempre sabe el tamaño de un puntero, que es lo que tú realmente quieres usar; por ello, con decirle que stTree es un tipo válido, no necesita mas.

Una última cosa: en C++, no es necesario hacer typedef para las clases (ni para las struct, que son un tipo de clase):

struct Tree;

struct DataComplex {
  qint8 numberA;
  qint16 numberB;
  qint32 numberC;
  qint64 numberD;
  qint32 *intList;
  qint8 *byteList;
  DataComple *list;
  int listlen;
  QString string;
  int mode;
  float floatv;
  double doublev;
  Tree *que_chulo[1024];
};

struct Tab {
  QString name;
  DataComplex value;
};

struct Tree {
  QString name;
  Tab data[255];
  int data_n;
  Tree *branches[255];
  int branches_num;
};

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.