I'm having a problem with some structs in a program I am writing. What I am trying to do is use pointers to a struct type within a struct that uses that type as well. For example:
struct struct2;
struct struct1
{
struct2 * struct2_pointer;
}
struct struct2
{
struct1 * struct1_pointer;
}
This ability is a new concept to me, for two structs to have access to one another. Before I though that if for example struct1 knew struct2, then struct2 couldnt know struct1, but then I read about adding the "struct scruct1;" line so that it could know it.
Anyways, that is a primer for the problem I am having. Here is my actual code:
My problem is that when I debug the code, it gives me an error in the "set" method, at the line "duration.set(&effect_template->duration,&creature->adjusters,effect_template->id.type);". It says that CREATURE is an undefined type, although it has no problem with the "CREATURE * creature;" line at the top of the struct, and no problem with the "creature = new_creature;" line within the set method. Any ideas what might be causing this?
I don't know what you expect, I provided a pretty simple problem and explained it the best I could, there's really nothing else I could show you. If you don't know what the problem is related to thats fine but dont tell me to "Post code which produces the error but also compilable and short" because it won't compile in the first place. You said earlier that I need to define CREATURE but when I asked you to explain that you opted not to.
Sorry, but you didn't post "EFFECT_TEMPLATE.h" and more importantly "CREATURE.h" where, as you say, CREATURE class is defined and as there is a problem with the CREATURE class, it is essential to know how it is implemented.
Try posting "CREATURE.h", see if there is a problem with that.
Also, why are you forward declaring CREATURE after it's already defined in "CREATURE.h"?
I can assure you that there is no problem with CREATURE.h or EFFECT_TEMPLATE.h, this problem didn't show up until I tried giving CREATURE_EFFECT access to the CREATURE struct. The problem is CREATURE has access to CREATURE_EFFECT, so I read somewhere that I needed to add the line "struct CREATURE;" so that CREATURE_EFFECT could know what CREATURE is... I don't really understand how this works, it is a new concept to me, but it comes down to wanting multiple classes to know each other. If I don't add the line "struct CREATURE;", it has no idea what CREATURE is and gives me errors everytime CREATURE is used in CREATURE_EFFECT
Because you are using information (member variable 'adjusters'), which is only available in the definition of the class, so the struct definition needs to be visible for complier when it compiles CREATURE_EFFECT::set().
Thanks for helping me with that! I've got one other problem now though.
I have a struct named EFFECT_ACTION, which has a set of many ACTION's (ACTION being another struct). Compiler is telling me now that ACTION is not defined. I dont think that the code for EFFECT_ACTION is important to show you because it is simply what i decribe, but here is ACTION.h:
I have commented out the EFFECT_TEMPLATE and PORTAL_TEMPLATE stuff, because that is the root of the problem. Only leaving the include lines for EFFECT_TEMPLATE and PORTAL_TEMPLATE at the top, the error still exists. Let me explain how EFFECT_TEMPLATE is build. It has an EFFECT_ACTIONS type, which is comprised of EFFECTS_PER_ACTION number of EFFECT_ACTION types. EFFECT_ACTION like said before is comprised of ACTION types. So it looks like this: EFFECT_TEMPLATE->EFFECT_ACTIONS->EFFECT_ACTION->ACTION. For some reason it doesnt like when I try to give ACTION access to EFFECT_TEMPLATE. PORTAL_TEMPLATE is similiar to EFFECT_TEMPLATE but it goes one level higher and has it's own EFFECT_TEMPLATE pointer within it. I don't know where to begin with solving this problem, would I need to take all methods definitions out of every header file and put them into .cpp files?
Wow, you have some very big/complicated project going on.
I don't know where to begin with solving this problem, would I need to take all methods definitions out of every header file and put them into .cpp files?
It is usually good practice to keep the implementation separated from the definition for two reasons :
1. Makes compiling faster.
2. Makes your code more readable (to others).
If you define your functions in the implementation then the complier automatically tries to inline them just like if they were defined with the keyword inline.
Try put your functions into separate cpp files, then if the problem is still present, try rethink your class hierarchy. It's usually a bad practice to have cross references (but there are cases where it's the only solution).
Ok, I'll try that. I have quite extensively thought out my class heirarchy, and it's at a point where it is very powerful and well organize. I think it's pretty good right now, and I think I am running into some of those rare cases where cross referencing is needed. Thanks for your help. I won't mark this thread solved until I seperate my implementation and see that it solves the problem.
btw seperated implementation makes my code compile slower..
Really?
Do you compile every cpp file every time, even if it is not changed? The object (.o) files don't depend on the other object files, so if a cpp wasn't changed then you don't have to recompile it (just link it).