.h and .cpp with array of structures

0

Good, I need to create a header with an array of structures and a .cpp where the methods are implemented, and it has to have a struct, and create an array of that struct, and then from another file create that array and make the operations with the same, is a kind of table that contains a name, a type and a value in the .h I have:

#ifndef TABLA_H
#define TABLA_H


union dato
{
    int entero;     
    float real;
    int booleano; 

};

typedef struct mitabla {
     std::string Nombre;
     int Tipo;
     dato Valor;
};

mitabla tablasimb[10];


extern void nuevavarentera(int cont,std::string nombre,int tipo,int valor);


#endif

in table cpp at the moment a single method to try:

#include "tabla.h"

void tabla :: nuevavarentera(int cont,std::string nombre,int tipo,int valor){

    std::transform(nombre.begin(), nombre.end(), nombre.begin(), ::toupper);

    tablasimb[cont].Nombre=nombre;

    tablasimb[cont].Tipo=tipo;

    tablasimb[cont].Valor.entero=valor;}

and in the main file in the main I want to create it but I can not:

int main(int argc, char** argv){


    tabla mitabla;
    mitabla.nuevavarentera(0,"mivariable",0,2);


}

Some idea of how to create this array of structures in the header and create it from the main in another file (I do the includes "tabla.h")

    
asked by Jpachecod 16.05.2017 в 17:13
source

1 answer

0

Your question is very general, and you also have conceptual errors, so I will try to solve your doubts and also clarify some concepts.

  

I need to create a header with an array of structures and a .cpp where the methods are implemented, and it has to have a struct, and create an array of that struct, and then from another file create that array

In the .h you want to define the TYPES of variables. This is correct, it is the general idea of having a header file.

The conceptual error that you have here, is that an array of elements of a type, is not a new type, it is a declaration of multiple instances of a type. That's why you should not declare it in .h .

What you can do is declare a struct within the .h , which contains an arrangement you want. This arrangement will be instantiated, only if the structure that contains it is instantiated.

Your code would look something like this:

In tabla.h

#ifndef TABLA_H
#define TABLA_H


union dato
{
    int entero;     
    float real;
    int booleano; 

};

typedef struct mitabla {
     std::string Nombre;
     int Tipo;
     dato Valor;
} ALIAS;        //Faltaba un alias

struct TablaArray {
    mitabla tablasimb[10];
};

Add the methods you want to Array Table.

Now you can instantiate in your code struct TablaArray and then you will be instantiating the arrangement you want. Then main would be:

int main(int argc, char* argv[]) {

    TablaArray tabla; //TablaArray: tipo dato --- tabla: nombre
    //Lo que desees hacer

}

Notice that in main you were trying to create the variable of type tabla , which was not defined.

    
answered by 16.05.2017 в 18:39