Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 02-19-2010, 02:31 PM   #1
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Incomplete Type

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?
__________________

JogaBonito1502 is offline   Reply With Quote
Old 02-22-2010, 05:51 PM   #2
Guru
 
JogaBonito1502's Avatar
 
Join Date: Jan 2008
Location: U.S.
Posts: 7,841
Default Re: Incomplete Type

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.
__________________

JogaBonito1502 is offline   Reply With Quote
Old 02-23-2010, 02:56 AM   #3
Fully Optimized
 
Š88's Avatar
 
Join Date: May 2005
Posts: 4,775
Send a message via MSN to Š88
Default Re: Incomplete Type

Quote:
Originally Posted by JogaBonito1502 View Post
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
__________________
Killing under the cloak of war is no different than murder

Kein Alkohol ist auch keine Lösung!
Š88 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 10:34 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0