Something weird with Includes

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

Recommended Posts

Hey, I have a struct called hasObject in "structs.h" and a class named Object in "Object.h". The class uses the struct and vice-versa: //structs.h struct hasObject { Object* pobject; Number numOf; }; //object.h bool addHas(hasObject hasob); The problem is visual c++ 2008 express doesn't recognize Object as a class. Actually, in the editor after I write Object it recognizes it in the main function, but not if I write it just after main(). I have tried Object::Object but it still doesn't recognize. I checked hundred times that the files are included correctly. The error is this: error C2143: syntax error : missing ';' before '*' on this: Object* pobject; Any help will be appreciated

Share on other sites
Did you include Object.h in structs.h?

Share on other sites
Quote:
 Original post by radagaisusI have a struct called hasObject in "structs.h" and a class named Object in "Object.h". The class uses the struct and vice-versa://structs.hstruct hasObject { Object* pobject; Number numOf;};//object.hbool addHas(hasObject hasob);
In general, you should avid circular dependencies like this wherever possible. You should be able to make this work by #including structs.h in Object.h, and providing a forward declaration of Object in structs.h.

Share on other sites
You may need to forward declare Object in structs.h, like this:

//structs.hclass Object; // this forward declares Objectstruct hasObject{    Object* pobject;    Number numOf;};

This link has a nice little example.

ninja'd++;

Share on other sites
Why hasObject and not std::vector<Object>?

Share on other sites
Thank you!

The problem was indeed using circular declarations. I added "class Object;" and the problem was solved. Thank you for the quick reply!

I'm using struct hasObject since it stores not only a pointer to an object but also a number of how many times it can be generated, I'm writing a script language and this ensures that I can write in it, for example:

infinite is a number(+); //which means there can be infinite number of legs.
creature has #infinite leg[s]; //[] - is a comment.

And then I'll have inheritance. I could write: 'spider is a creature' and it will inherit all creature's traits and it can modify this property of it.

Thank you.