Archived

This topic is now archived and is closed to further replies.

class declrations and #include

This topic is 5807 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Errr...wrong example...here is the correct one.

class A
{
B var1;
};

// in other file
class 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

Share this post


Link to post
Share on other sites
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!


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just swap round the order of the A and B declarations and you''ll be sweet

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites