forward declaration in Headerdateien, "has incomplete type"



  • Ich hab erhalte die Fehlermeldung

    a.hpp:10: error: field ‘b_’ has incomplete type
    

    für folgendes Minimalbeispiel:

    main.cpp

    #include "a.hpp"
    
    int main()
    {
    }
    

    a.hpp

    #ifndef A_HPP
    #define A_HPP
    
    class A;
    
    #include "b.hpp"
    
    class A
    {
    	B b_;
    };
    
    #endif
    

    b.hpp

    #ifndef B_HPP
    #define B_HPP
    
    class B;
    
    #include "a.hpp"
    
    class B
    {
    };
    
    #endif
    

    b.cpp

    #include "b.hpp"
    

    Das ganze tritt auf beim Compilieren von b.cpp, bei der main.cpp gibt's keine Probleme. Wie lässt sich das beheben? 😞

    mfg.



  • Vielleicht indem du mal weniger zyklische Abhängigkeiten einbaust? 😉

    Schau dir doch mal die Datei an, die dir der PP ausspucken wird:

    // main.cpp
    class A;
    
    class B;
    
    class B
    {
    };
    
    class A
    {
        B b_;
    };
    
    int main ()
    {
    }
    
    // b.cpp
    class B;
    
    class A;
    
    class A
    {
        B b_;  // Oha, hier kennt der Compiler noch gar nicht die Implementierung von B!
    };
    
    class B  // Und nu ists zu spät ... traurig.
    {
    };
    

Anmelden zum Antworten