1

I have a problem. In my Qt project I'm having an error. Tt says:

multiple definition of estructura

I think it is because I'm using too many "#includes", but I don't know how to fix it.

This is my program:

Struct.h

 #ifndef STRUCT_H
    #define STRUCT_H

    #include <stdio.h>
    #include <string>

    struct Circulo{
        std::string nombre;
        int capacidad;
        int statusMujer[4];
        int hijos[4];
    };

    struct Circulo estructura[30];

    #endif // STRUCT_H

Logica.h

 #ifndef LOGICA_H
 #define LOGICA_H
#include <iostream>
void madresStatusCodificados(std::string , int , int[]);
void ListadoDeCirculos(int, int , std::string []);
std::string CirculoMayorCapacidad(int );
int NinnosEnCirculosconStatus(int , int );
void Listado(int);
#endif // LOGICA_H

interfaz.cpp

#include <Interfaz/Interfaz.h>
#include <Logica/Defecto.h>
#include <Interfaz/Menu.h>
#include <Logica/Struct.h>
#include <iostream>
using namespace std;
 void Principal(){
                  cin>>estructura[i].statusMujer[3];
                  cout<<"Escriba la cantidad de hijos con madres desconocidas"<<endl;
                  cin>>estructura[i].hijos[3];
                  i++;
              }else{
                  break;
              }
 }
}

Logica.cpp

#include <iostream>
using namespace std;
//#include <Logica/Logica.h>
#include <Logica/Struct.h>

void madresStatusCodificados(string t,  int h , int k[]){
for(int i=0;i<h;i++){
    if(estructura[i].nombre==t){
       k[0]=estructura[i].statusMujer[0];
       k[1]=estructura[i].statusMujer[1];
       k[2]=estructura[i].statusMujer[2];
       k[3]=estructura[i].statusMujer[3];
    }
   }
}

void ListadoDeCirculos(int g,int u,string h[]){

    for(int i=0;i<u;i++){
        if(estructura[i].statusMujer[g-1]!=0)
        h[i]=estructura[i].nombre;
    }
    }


string CirculoMayorCapacidad(int k){
    int n=-1;
    string l;
    for(int i=0;i<k;i++){
        if(estructura[i].capacidad>n){
            n=estructura[i].capacidad;
            l=estructura[i].nombre;
        }
    }
    return l;

}

int NinnosEnCirculosconStatus(int a,int b){
    int h=0;
for(int i=0;i<b;i++){

    h=h+estructura[i].hijos[a-1];
}

return h;
}


void Listado(int y){
    for(int i=0;i<y;i++){
        cout<<"NOMBRE DEL CIRCULO    CAPACIDAD  Madres solteras Madres trabajadoras Madres caso social Madres desconocidas  "<<endl;
       cout<<estructura[i].nombre<<""<<estructura[i].capacidad<<estructura[i].statusMujer[0]<<estructura[i].statusMujer[1]<<estructura[i].statusMujer[2]<<estructura[i].statusMujer[3]<<endl;
    }
}

I am getting this error: Errors

scopchanov
  • 7,966
  • 10
  • 40
  • 68
  • http://idownvotedbecau.se/imageofanexception/, and no pointer from you which line causes the error. Also http://idownvotedbecau.se/nomcve/ – Murphy Dec 30 '17 at 09:42

2 Answers2

1

You have defined a variable named "estructura" in several files, the error tells you that it was first defined in Struct.h being called #included in Logica.cpp,

struct Circulo estructura[30];

you have another definition the the file Defecto.h or a file included by Defecto.h

Immac
  • 466
  • 1
  • 5
  • 16
1

Move the definition of estructura in a cpp file of your choice, then declare it as extern wherever you need it. And you can omit the struct keyword, in c++.

In a struct.cpp file:

#include "Struct.h"

Circulo estructura[30];

In your Logica.cpp and Interfaz.cpp files:

#include "Struct.h"

extern Circulo estructura[30];

Here is a long and detailed explanation of your issue.

p-a-o-l-o
  • 9,807
  • 2
  • 22
  • 35
  • Do you mean,put the estructura declaration in a .cpp file,and declare this declaration as extern??Thanks in advance – cristian sori Dec 27 '17 at 23:19
  • Already did that,so,now,do i have to declare estructura in all .cpp files?Thanks in advance – cristian sori Dec 27 '17 at 23:25
  • I edited the answer to better explain what you have to do. Please read the answer in the link I provided to understand **why** you have to do so. – p-a-o-l-o Dec 28 '17 at 08:22