Skip to main content
gamedev.net
🔒 Locked

C++ class problem

Started by woytah 7 replies 950+ views
Original Post
woytah
woytah
I have got a file which contains 2 definitions: class FirstClass { SecondClass Member; }; class SecondClass { FirstClass Member; }; I think you see the problem now. When processing FirstClass, word: "SecondClass" is unknown. How to make it work?
dragongame
dragongame

class F1
{
class F2 member;
};

class F2
{
F1 member;
};
“Always programm as if the person who will be maintaining your program is a violent psychopath that knows where you live”
Ezbez
Ezbez
This seems risky at best, though. You see, FirstClass has a SecondClass which has a FirstClass which has a SecondClass which has a FirstClass which has a SecondClass which has a FirstClass which has a SecondClass, etc. Is this just an over simplification or is this what you are actually doing?
woytah
woytah
Hm... forward declarations seems not working. This is only a simplification of my problem. Somehow I've managed to move class declaration, and remove some members so now it works. But should I do if moving/removing will not be possible?
ToohrVyk
ToohrVyk
Quote:
Original post by woytah
Hm... forward declarations seems not working. This is only a simplification of my problem. Somehow I've managed to move class declaration, and remove some members so now it works. But should I do if moving/removing will not be possible?


Use references and pointers, which only require a forward declaration. References for pass-by-reference, const references for pass-by-value and pointers for internal storage are usually enough. Remember that if your design involves two classes containing each other (as opposed to simply referencing one another), you have a problem.

Fruny
Fruny
Ezbez has it right: a forward declaration cannot make this specific piece of code work. It only allows you to use the type in pointers, references and function prototypes, where no information about the type's actual size is needed.

class SecondClass;class FirstClass{   SecondClass<font color=red>*</font> Member;};class SecondClass{   FirstClass Member;};
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
woytah
woytah
So I will use pointers then! THANK YOU ALL FOR HELP!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.