C ++ - Error loading data in a struct array located in a header file

-1

I am very new to c ++ and I am having some problems with a task, the same is detailed below:  I have a struct and a function to load data into it. The program compiles correctly if all the code is in the same main.cpp, but I need it in different files (Main file, struct file, function file, etc).

The code is as follows:

struct.cpp

    struct Coleccion
    {
     int enteros;
     double coma;
     char caracter;
    } ;
     Coleccion colection[5] ;

struct.h

    extern Coleccion;
    extern enteros;
    extern coma;
    extern caracter; 
    extern colection;

altaint.cpp

   #include <iostream>
   #include "struct.h"

   void altaint()
   {
    for (int i=0; i < 5; i++)
    {
     std::cout<<"Ingrese un valor entero: ";
      std::cin>>colection[i].enteros;
    }
   }

main.cpp

   #include "altaint.cpp"

   int main ()
    {
     altaint();
     return 0;
    }

By doing this, the compiler returns the error "Invalid indirection" in

    std::cin>>colection[i].enteros;

right in

    "[i]"

Before anyone asks why I do the struct.cpp and the struct.h in this way, it is because the program actually has many more functions to load data into the struct, as well as others to see the content of the arrays, and others, when I included the struct.cpp in each function that required access to the struct, the compiler returned an error of type "more than one definition for struct coleccion" or something like that, and that is why I defined the struct in the .cpp and declare the extern variables in the .h.

Well, sorry for the inexperience of the code, and thanks for reading.

    
asked by Frodo 02.09.2017 в 23:49
source

1 answer

0

Hello, your code has some rare datalles, for example, the struct can naturally be in the header.

On the other hand, if you want to modulate your code then create its respective header to the file altaint.cpp and place the prototyps of the functions there.

One way to do what I tell you is like this:

Header Struct

#ifndef STRUCT_H
#define STRUCT_H

struct Coleccion
{
    int enteros;
    double coma;
    char caracter;

};


#endif // STRUCT_H

Header altaint

#ifndef ALTAINT_H
#define ALTAINT_H
#include "struct.h"

void altaint();
void mostrarValores();
extern Coleccion colection[];

#endif // ALTAINT_H

Implementation of altaint

#include "altaint.h"
#include<iostream>

Coleccion colection[5];

void altaint(){
    for (int i=0; i < 5; i++)
    {
        std::cout<<"Ingrese un valor entero: ";
        std::cin>>colection[i].enteros;
    }
}

Main

#include "altaint.h"
using namespace std;

int main()
{
    altaint();
    return 0;
}
    
answered by 03.09.2017 в 16:43