Something weird with Includes
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
Quote:Original post by radagaisusIn 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.
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);
You may need to forward declare Object in structs.h, like this:
This link has a nice little example.
[edit]
ninja'd++;
//structs.hclass Object; // this forward declares Objectstruct hasObject{ Object* pobject; Number numOf;};
This link has a nice little example.
[edit]
ninja'd++;
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>
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
Popular Topics
Advertisement