Sign in to follow this  
Hydraxy

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

Recommended Posts

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 this post


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


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


Link to post
Share on other sites
Quote:
Original post by SnotBob
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 { ... };


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 this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Forward 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 this post


Link to post
Share on other sites
Quote:
Original post by SnotBob
Quote:
Original post by Driv3MeFar
Forward 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 this post


Link to post
Share on other sites
Quote:
Original post by Enigma
Quote:
Original post by SnotBob
Quote:
Original post by Driv3MeFar
Forward 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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this