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.