myProgram.cpp:
Code:class childClass { parentClass parent; //reference to class that created this child childClass(parentClass p) { parent = p; } }; class parentClass { childClass child(this); };
This would work fine in Java, which I'm more accustomed to, but I need to do it in C++. The problem being that C++ compilers read classes sequentially and this code gives the error "error C2061: syntax error : identifier 'parentClass'" because it does not know what parentClass is yet. All the advice on the internet says to create a forward declaration for parentClass BEFORE the definition for childClass, and then keeping the definition for parentClass after childClass, such:
But this code then gives the error: "error C2079: 'myProgram:arent' uses undefined class 'parentClass'"Code:class parentClass; class childClass { parentClass parent; //reference to class that created this child childClass(parentClass p) { parent = p; } }; class parentClass { childClass child(this); };
I've tried swapping around the classes, too, so that parentClass comes before childClass with a forward declaration for childClass. This gives the same error. I've also tried putting the classes in seperate *.cpp files. I must admit I don't really know how to do this, but the way I did it resulted in the same error (...uses undefined class...).
What do you think? I'm a decent Java and C# programmer but C++ is just taking the .........