Incomplete type error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • unclefester
    New Member
    • Mar 2008
    • 12

    Incomplete type error

    I have two classes: Test1 and Test2. Test1 has a field of data type Test2, & vice versa. I need some help in avoiding the incomplete type error ("Error: field has incomplete type").

    Test1.h
    Code:
    #ifndef _TEST1_
    #define _TEST1_
    
    #include "Test2.h"
    
    using namespace std;
    
    class Test1
    {   public:
           Test2 test2Obj;       
    }
    
    #endif
    Test2.h
    Code:
    #ifndef _TEST2_
    #define _TEST2_
    
    #include "Test1.h"
    
    using namespace std;
    
    class Test2
    {   public:
           Test1 test1Obj;       
    }
    #endif
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by unclefester
    I have two classes: Test1 and Test2. Test1 has a field of data type Test2, & vice versa. I need some help in avoiding the incomplete type error ("Error: field has incomplete type").

    Test1.h
    Code:
    #ifndef _TEST1_
    #define _TEST1_
    
    #include "Test2.h"
    
    using namespace std;
    
    class Test1
    {   public:
           Test2 test2Obj;       
    }
    
    #endif
    Test2.h
    Code:
    #ifndef _TEST2_
    #define _TEST2_
    
    #include "Test1.h"
    
    using namespace std;
    
    class Test2
    {   public:
           Test1 test1Obj;       
    }
    #endif
    You can't do that: if you recursively define Test1 and Test2 in terms of each other
    a Test1 contains a Test2 which contains a Test1 again which contains a Test2,
    ad nauseam. Better think of pointers or references to Test1 and Test2 instead.

    kind regards,

    Jos

    Comment

    • unclefester
      New Member
      • Mar 2008
      • 12

      #3
      Thanks so much for the advice Jos =)

      Comment

      Working...