Using a variable that hasn't been declared yet...

Recommended Posts

Hydraxy    122
I want to be able to use a variable that hasn't been declared yet... I think that there is some keyword or way to do this (as I knew someone who did this I think) but I'm not sure how exactly to go about doing it... Anyway, basically what I want is this. (In C++) class city { public: some variables here... void produce(int &, int &, army); }; class army { public: some bariables here... void someotherfunction(int &, int &, city); } As you can see even if I reorder them I try to use a type city before it is declared (or a type army). I want to know if there is a way to tell the compiler that something will be declared... but its just not yet. Hope that makes sense. Anyway, thanks for your help.

Share on other sites
SnotBob    202
You can't refer to a variable before it's declared, but what you're trying to do is referring to a class before it's defined. Just declare your classes before definitions:
class army;class city;class army { ... };class city { ... };

Share on other sites
Black Knight    769
Search for "forward declaration"

You can write the army in the function declaration like

class Army;
class City{
void produce(int i,Army* army);
};

You will need to include army.h in your city.cpp file.

Share on other sites
Driv3MeFar    1080
Quote:
 Original post by SnotBobYou can't refer to a variable before it's declared, but what you're trying to do is referring to a class before it's defined. Just declare your classes before definitions:class army;class city;class army { ... };class city { ... };

Forward declarations work as long as you only use pointers to the forward-declared class (otherwise, the full class definition must be available so its size is known):

class army;

class city
{
public:
some variables here...
void produce(int &, int &, army*);
};

class army
{
...
};

Share on other sites
SnotBob    202
Quote:
 Original post by Driv3MeFarForward declarations work as long as you only use pointers or references to the forward-declared class (otherwise, the full class definition must be available so its size is known)

True.

Share on other sites
Enigma    1410
Quote:
Original post by SnotBob
Quote:
 Original post by Driv3MeFarForward declarations work as long as you only use pointers or references to the forward-declared class (otherwise, the full class definition must be available so its size is known)

True.
class army;class city{	void produce(int &, int &, army);};
is fine. A full definiton of army is only required when you define city::produce.

Σnigma

Share on other sites
outRider    852
Quote:
Original post by Enigma
Quote:
Original post by SnotBob
Quote:
 Original post by Driv3MeFarForward declarations work as long as you only use pointers or references to the forward-declared class (otherwise, the full class definition must be available so its size is known)

True.
class army;class city{	void produce(int &, int &, army);};
is fine. A full definiton of army is only required when you define city::produce.

Σnigma

Or when you call city::produce.

Share on other sites
Hydraxy    122
Alright - Thank you all - I knew it was something simple I was just blanking on.

Back to coding =)