2 files that both depend on each other

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

2 files that both depend on each other

Post by cameron » July 9th, 2013, 9:31 pm

How do I get 2 classes to work that both depend on each other?( they are also in different files )
Is there anyway to have them both include each others .h file?
I get hundreds of errors having them include each other.
I am making a MenuWindow class that holds all the different menus and puts it into a class.
Computer too slow? Consider running a VM on your toaster.

Shaki
Posts: 104
Joined: June 13th, 2012, 12:20 am

Re: 2 files that both depend on each other

Post by Shaki » July 9th, 2013, 10:15 pm

Yes, you can define both classes and just have them interact, but the best idea is to have a class with both inside, it just makes things simpler.

You need to make sure however, that when you use a class which is defined before another, that you pre-define it (or whatever it's called)
IE:

Code: Select all

//this is temp2.h
#include "Temp.h"

class A;

class B{
public:
B();
~B();
private:
A a;
};

Code: Select all

//this is temp.h
#include "Temp2.h"
class A{
public:
A();
~A();
private:
B b;
};

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: 2 files that both depend on each other

Post by LuisR14 » July 9th, 2013, 10:19 pm

why not put them in the same file?
and well for 2 classes to refer to each other the later defined class has to be declared before the 1st defined class, ex:

Code: Select all

class Obj2;

class Obj1 {
	Obj2 obj2;
};

class Obj2 {
	Obj1 obj1;
};
edit: lol, seems i was late :P
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: 2 files that both depend on each other

Post by cameron » July 10th, 2013, 1:45 am

Thanks both of you.
Computer too slow? Consider running a VM on your toaster.

Post Reply