class declrations and #include

Started by
4 comments, last by mse61 22 years, 1 month ago
I define one class in one file and another in a second file like this:

class A
{
    B var1;
};

// in second file
class B
{
    A var2;
};
 
I''ve tried to declare the class ahead of the implmentation like this:

class A;
class B
{
    A var2;
};
 
but i still get errors about undefined classes. This problem is really eating up important time, and I''m out of ideas. HELP! ~mse61 "Today UNIX command is: exop - execute operator" ICQ: 122419859 AIM: ThePhantomTIE e-mail: randlem@bgnet.bgsu.edu
++mse61--ICQ: 122419859AIM: mse6102
Advertisement
Your first example is impossible since it takes infinite amounts of memory to store. A has a B which has an A which has a B, et cetera. You can have a pointer to B in A, and vice versa, using the second method.

Errr...wrong example...here is the correct one.
class A{   B var1;};// in other fileclass B{   float array[4][4];};void Some_Function(A var2); 


~mse61

"Today UNIX command is: exop - execute operator"

ICQ: 122419859
AIM: ThePhantomTIE
e-mail: randlem@bgnet.bgsu.edu
++mse61--ICQ: 122419859AIM: mse6102
Forward declarations can only be used for reference or pointers because if you write( say there is a class called myClass)

class OtherClass
{
myClass A;
}

then the compiler needs to know the correct size of myClass to be able to correctly compiler OtherClass, but

class OtherClass
{
myClass* A;
}

or

class OtherClass
{
myClass& B;
}

Then, it does not need to know the full size of myClass, because they are declared as pointers and pointers have a known size.

I do not think I have been clear enough, but that''s life!


just swap round the order of the A and B declarations and you''ll be sweet
Anyway, the pointer/reference is the best solution. If you only have two classes the problem is still easy to solve (swap). But what about 10-20 classes?

DworD
DworD

This topic is closed to new replies.

Advertisement