Incomplete Type

Messages
7,841
Location
U.S.
USING C++
I am trying to use a class inside another class. However, when I define the variable from the first class in the second I get an error saying, "Incomplete type". This is what my code looks like.

Header file A.h
Code:
class A
{
Private:
          int rows;
          int columns;
          int array1[];
Public:
          void build();
};

Then, I include that header file in a second header file, B.h, which defines my second class.

Code:
#include "A.h"
class B
{
Private:
          int m;
          int array2[];
          A variable;
Public:
          void test();
};

On my main source file I have:
Code:
#include "A.h"
#include "B.h"
#include <iostream>
using namespace std;

int main()
{
           int i[];

           //random stuff here

           return 0;
}

Now the problem is that the array array2 gives me errors saying incomplete type. If I delete A.h and not define "variable", then "i" gives me an error with incomplete type on main(). What's wrong here?
 
I have an update on this issue. Instead of using a class within another class, I'm trying to create a dynamic array using new/delete in a class function. However, I keep getting this incomplete type error. Am I supposed to declare the dynamic array in the header file definitions? I currently have it in the actual code for the function (which is located in the source file). The class definition header file is included in the source file. Do I need to make a second declaration of the form:

Code:
class A;

before int main() in my source file? By that I mean with all my #include's.
 
I have an update on this issue. Instead of using a class within another class, I'm trying to create a dynamic array using new/delete in a class function. However, I keep getting this incomplete type error. Am I supposed to declare the dynamic array in the header file definitions? I currently have it in the actual code for the function (which is located in the source file). The class definition header file is included in the source file. Do I need to make a second declaration of the form:

Code:
class A;

before int main() in my source file? By that I mean with all my #include's.

It's was a bit difficult following exactly what you were explaining so I'll try to answer what I think you asking.

In your header file you want to have a pointer declaration (unless of course it is only to be accessed by one particular function then you don't have to have it in your header). That is, if you want to create a dynamic array of ints use this:

Code:
int *x;

Then in your implementation file for the function you create a new array by using (for example):

Code:
x = new int[10];

which creates a dynamic array of size 10.

Ok, you probably already knew that but just making sure.

You only need to include (assuming in this case your header file is called A.h):

Code:
#include "A.h"

Then in your main() create a new object using:

Code:
A objectName;

to access your class members and functions.

edit - k, after reading again I don't think I answered any of your questions lol
 
Back
Top Bottom