Something weird with Includes

Started by
4 comments, last by radagaisus 15 years, 4 months ago
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
Advertisement
Did you include Object.h in structs.h?
Quote:Original post by radagaisus
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);
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.

[edit]

ninja'd++;
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Why hasObject and not std::vector<Object>?
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; //[] - is a comment.<br><br>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.<br><br>Thank you.<br>

This topic is closed to new replies.

Advertisement