Incomplete type error

Mar 16, 2008 at 9:23am
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
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef _TEST1_
#define _TEST1_

#include "Test2.h"

using namespace std;

class Test1
{   public:
       Test2 test2Obj;       
}

#endif 


Test2.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef _TEST2_
#define _TEST2_

#include "Test1.h"

using namespace std;

class Test2
{   public:
       Test1 test1Obj;       
}
#endif 
Mar 16, 2008 at 12:03pm
Your definition of Test1 and Test2 are not possible. You probably want the fields to be pointers, like this:

1
2
3
4
5
6
class Test2; // Forward declaration because you can't have both files include the other

class Test1 {
public:
    Test2* test2Ptr;
};


1
2
3
4
5
6
#include "test1.h"

class Test2 {
public:
    Test1* test1Ptr;
};
Mar 16, 2008 at 2:09pm
Thanks for the advice ropez :)
Topic archived. No new replies allowed.