0

I´m building a program with several classes (data structures like stacks, lists,etc). There is some class (Concesionario) that i need to use in another (ListaE). The class ListaE uses another class called NodoListaE, which uses two pointers, one to the value of the object (Concesionario) and another to the next position of the list (siguiente).

#ifndef NODOLISTAE_HPP
#define NODOLISTAE_HPP
#include "Concesionario.hpp"

class Concesionario;
class ListaE;
class NodoListaE
{
    public:
        NodoListaE(Concesionario* conc, NodoListaE* sig = NULL);

    private:
        Concesionario* conc;
        NodoListaE* siguiente;
    friend class ListaE;
};
typedef NodoListaE* pnodoListaE;
#endif // NODOLISTAE_HPP

#ifndef LISTAE_HPP
#define LISTAE_HPP
#include "NodoListaE.hpp"
#include "Automovil.hpp"

class Automovil;
class NodoListaE;
class ListaE
{
    private:
        NodoListaE* primero;

    public:
        ListaE();
        void enlistarOrden(Automovil* automovil);

};

#endif // LISTAE_HPP

#ifndef CONCESIONARIO_HPP
#define CONCESIONARIO_HPP
#include <string>
#include "ListaE.hpp"

class ListaE;
class Concesionario
{
    public:
        Concesionario();
        ~Concesionario();
        std::string mostrar();
        void setZona(std::string letra);
        void setNum();
        int getNum();

    private:
        int nc=2;
        int num_conc;
        std::string zona;
        int generadorNumsIntervalo(int min, int max);
        ListaE automoviles;//ERROR HERE

};

#endif // CONCESIONARIO_HPP

All the cpp files are not implemented (empty constructor and destructor). The compiler I´m currently using is MINGWx64.

I´ve tried using forward declarations and it worked for the rest of the classes but not for this one. The program throws the following error in the **Concesionario ** hpp file: include\Concesionario.hpp|22|error: field 'automoviles' has incomplete type 'ListaE'| Concesionario is implemented in other classes and the program runs perfectly.

Example of another class implementing Concesionario

#ifndef ARBOL_HPP
#define ARBOL_HPP
#include <iostream>
#include "NodoArbol.hpp"
#include "Concesionario.hpp"
using namespace std;


class Arbol {

  public:
   Arbol();
   void Insertar(Concesionario* concesionario);

  private:
   pnodoArbol raiz;
   pnodoArbol actual;
   int contador;
   int altura;
   bool Vacio(pnodoArbol nodo);

};


#endif // ARBOL_HPP

I`ve also tried deleting this class and creating another one from 0 but the error remains. Any solution to this problem? Thank you very much.

  • 2
    A forward declaration can "work" only if the data type is used by pointer or reference. `ListaE automoviles` is not such a usage. You need to either use `ListaE*`/`ListaE&` or #include the relevant header file. – wohlstad Dec 12 '22 at 17:57
  • Looks like you have a circlular header loop: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Dec 12 '22 at 20:22
  • Somewhere on this site there is (or was) a question whose answer was that if you both `#include "X.hpp"` and forward declare `class X;` then you are doing it wrong. As I recall, that other question also nicely included the *full* error message, including the trace of where the error occurred. It turned out the person asking the question had misread that trace. Probably the same misreading here, but there is not enough information in the question to be sure. Still... Where is that question hiding? – JaMiT Dec 13 '22 at 07:43
  • 1
    @wohlstad thank you so much! This definitively help me out to find out what was wrong and finally I could make it work right. – Adrián Fernández García Dec 13 '22 at 10:35

0 Answers0