   Problem due to inclusion
  Hi, I'm facing a small problem right now. It would be more clear if I show an example://File1.hclass MainClass{...};//File2.hclass OtherClass{private:MainClass* m_pMainClass;};The problem is that I can't include File1.h into File2 because then I get about a 100 errors. Is there any way I can tell File2 that MainClass is defined in some other file? Thanks.   
  What you want is known as a forward declaration. Simply put the line class MainClass; at the top of File2.h, and you're all set [smile]Note that a forward declaration can only be used if you use exclusively pointers and/or references to the declared class. You can't, for example, forward declare ClassFoo and then have a ClassBar with a ClassFoo member variable. This is because you haven't told the compiler anything about ClassFoo - just that it exists. That's enough information to create a pointer or reference, but not enough to do anything more heavy-duty.You may also want to look into include guards for when you need to include one header from another.   ;  You can use an incomplete type if you only need to use a pointer to the object.  Then include your class definition header in the C++ file.class OtherClass{private:class MainClass* m_pMainClass;};   ;Quote:Original post by ApochPiQWhat you want is known as a forward declaration. Simply put the line class MainClass; at the top of File2.h, and you're all set [smile]Note that a forward declaration can only be used if you use exclusively pointers and/or references to the declared class. You can't, for example, forward declare ClassFoo and then have a ClassBar with a ClassFoo member variable. This is because you haven't told the compiler anything about ClassFoo - just that it exists. That's enough information to create a pointer or reference, but not enough to do anything more heavy-duty.You may also want to look into include guards for when you need to include one header from another.Basically I simplified my example, I have about 20 header files, all of which start with #pragma once. I also had the forward declaration of the MainClass and it worked just fine, but now I need to actually use the MainClass stored in OtherClass in another file, and now the compiler throws an error saying that the MainClass is undefined, and it points me to the empty forwrd decleration.//File1.hclass MainClass{...};//File2.hclass OtherClass{protected:MainClass* m_pMainClass;};//File3.h#include "File2.h"class OtherClass2 : OtherClass{m_pMainClass->DoStuff(); //Compiler throws an error, saying m_pMainClass is undefined};;  Do you really have to DoStuff in the header?   ;Quote:Original post by theOcelotDo you really have to DoStuff in the header?Come on guys, do you want me to write out every everything? I'm not doing anything in the header, the header is declaring a function and in the function declaration (in a seperate cpp file) I'm accessing the m_pMainClass.   ;Quote:Original post by VanillaSnake21Quote:Original post by theOcelotDo you really have to DoStuff in the header?Come on guys, do you want me to write out every everything? I'm not doing anything in the header, the header is declaring a function and in the function declaration (in a seperate cpp file) I'm accessing the m_pMainClass.Well then, in that cpp file include the header in which you have the MainClass definition.   ;Quote:Original post by ApochPiQNote that a forward declaration can only be used if you use exclusively pointers and/or references to the declared class.Not 100% true. Forward-declared types may also appear as return types or parameter types of functions.#include <iostream>// forward declare structstruct MyClass;// forward-declared struct shows up as a return valueMyClass returnAnObject();// forward-declared struct shows up as parametervoid takeAnObject( MyClass anObject );// now follow up our forward-declaration with an actual definitionstruct MyClass{int A;float B;};// define our 2 functionsMyClass returnAnObject(){MyClass temp = {};temp.A = 66666;temp.B = 7.5f;return temp;}void takeAnObject( MyClass anObject ){std::wcout << L"A = " << anObject.A << L", B = " << anObject.B << L"\n";}// compiles and runs just fine (at least on MSVC++ 2008)int main(){takeAnObject( returnAnObject() );return 0;};  @DriveMeIsn't that the same thing as including it in the cpp's file header file? If that didn't work why would putting it in cpp make a difference?P.S The best solution would most likely involve me changing how the files are included, but like I said I have about 20 .h and abour 10 cpp files, and they all include each other. The file with the MainClass, includes ALL the other files, so I can't really include it anywhere (without errors). ;  If you're just going to ignore people's advice then why bother asking in the first place? Forward-declarations ARE the solution to your problem. Forward-declare MainClass in all headers in which the class merely shows up as- a pointer or reference- a parameter or return value of a function. Then include the header file in which MainClass is actually defined in each cpp that needs to have seen the full definition of MainClass. Avoid putting anything except function declarations and type definitions in your header files. Any actual code should go in the cpps.   