468,513 Members | 1,763 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

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
Expand|Select|Wrap|Line Numbers
  1. #ifndef _TEST1_
  2. #define _TEST1_
  3.  
  4. #include "Test2.h"
  5.  
  6. using namespace std;
  7.  
  8. class Test1
  9. {   public:
  10.        Test2 test2Obj;       
  11. }
  12.  
  13. #endif
  14.  
  15.  
Test2.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef _TEST2_
  2. #define _TEST2_
  3.  
  4. #include "Test1.h"
  5.  
  6. using namespace std;
  7.  
  8. class Test2
  9. {   public:
  10.        Test1 test1Obj;       
  11. }
  12. #endif
  13.  
Mar 16 '08 #1
2 3694
JosAH
11,448 Expert 8TB
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
Expand|Select|Wrap|Line Numbers
  1. #ifndef _TEST1_
  2. #define _TEST1_
  3.  
  4. #include "Test2.h"
  5.  
  6. using namespace std;
  7.  
  8. class Test1
  9. {   public:
  10.        Test2 test2Obj;       
  11. }
  12.  
  13. #endif
  14.  
  15.  
Test2.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef _TEST2_
  2. #define _TEST2_
  3.  
  4. #include "Test1.h"
  5.  
  6. using namespace std;
  7.  
  8. class Test2
  9. {   public:
  10.        Test1 test1Obj;       
  11. }
  12. #endif
  13.  
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
Mar 16 '08 #2
Thanks so much for the advice Jos =)
Mar 16 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Lou Pecora | last post: by
5 posts views Thread by Paul F. Dietz | last post: by
6 posts views Thread by Pushkar Pradhan | last post: by
5 posts views Thread by kj | last post: by
2 posts views Thread by Anthony Borla | last post: by
2 posts views Thread by Halid Umar A M | last post: by
10 posts views Thread by arcadio | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.