Sign in to follow this  

Weird Undeclared Identifier Problem

This topic is 2841 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've been trying to pin down the cause of this bug for most of the day but I just can't figure it out. It's kind of confusing to figure out when exactly it happens but I've observed a few interesting things. I have a class Plane with a vector of Weapon*, so plane.h includes weapon.h. weapon.h itself includes projectile.h because it has projectiles but this error seems to happen even when the projectile code is commented out so it seems to be due to headers. If weapon.h includes another file called map.h, or if it includes projectile.h and that includes certain headers, I get "error C2065: 'Weapon' : undeclared identifier" for the vector of Weapon* in plane.h. If map.h isn't included, it can all compile. If I don't use Weapon objects or pointers, there's no problem either. If I mess around, I can get the same problem with the Projectile class too where it's not recognized based on what headers are included. So what I'm wondering is in what situations might combining certain headers result in a class not being recognized? It's compiling without the headers so it seems to be some kind of bizarre conflict but those headers are all used in some other files. The 2 classes don't error on their own and it does seem that it's all due to the headers. I tried it just there, it compiled... I commented out "#include "map.h"" and it doesn't compile. I have no idea what's going on :(

Share this post


Link to post
Share on other sites
Quote:
I have a class Plane with a vector of Weapon*, so plane.h includes weapon.h.

I think you mean, "I have a class Plane with a vector of Weapon*, so plane.h forward-declares Weapon and does not include weapon.h". If you don't, read this. It sounds like what's going on is a circular include.

Share this post


Link to post
Share on other sites
Yeah, I've seen that article before, it's been quite helpful in the past.

I'm not sure what you mean exactly but as I said, plane.h has "#include "weapon.h"" at the top, and as for circular dependency weapon.h doesn't include plane.h.

As I mentioned though, the bizarre part is that plane.h can have a vector of Weapon* no problem if weapon.h doesn't have "#include "map.h"". If map.h isn't included in weapon.h, it seems to know exactly what weapon.h is, but putting map.h manages to make it an undeclared identifier.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Does map.h directly or indirectly include plane.h?


Hmmm... I'll have to see about that. I think it might quite indirectly...

Yep. map.h includes gameplay.h which includes another that includes plane.h.

You think that might be the problem?
Testing it out, it does compile if I remove the include of plane.h in gameplay.h.

So that means that... map.h indirectly includes plane.h and plane.h (which includes gameplay.h) also indirectly includes map.h.

That's circular then, isn't it? It's so different to the usual examples that I didn't notice anything especially since it only crept in due to a 3rd class.

Does that mean the only way around this is to put in a forward declaration for Weapon? How would you avoid something like this happening? It's quite a complex set up once you have a lot of includes and different files.

Share this post


Link to post
Share on other sites
Ok cool, thanks.

There's no real practical re-organization that could stop something like this then is there? I suppose you could butcher it and find some way but if you have files that cause this is it basically unavoidable?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sean_Seanston
Ok cool, thanks.

There's no real practical re-organization that could stop something like this then is there? I suppose you could butcher it and find some way but if you have files that cause this is it basically unavoidable?


Using forward declarations when you can (and including the other headers when you have to) is the "practical re-organization".

Share this post


Link to post
Share on other sites

This topic is 2841 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.

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