When you use references to a class in another classes header file (e.g. becuase the class uses the other class objects in function calls or as objects for its own use) then instead of including the other classes header file in the .h file, use the following forward class declaration instead:

	ref class OtherClassName;

(Typically you add it in the classes namespace at the top)

For instance, B is the first class the compiler encounters, and it includes an A^ member. What is A? The compiler hasn’t encountered any definition of A yet, so it issues an error (‘expected ; before ^’ being a typical error).

The solution is to make a forward declaration of A. At some point before the definition of B, add a line ref class A;

This gives the compiler the necessary information, that A is a class. We don’t know anything else about it yet, but since B only needs to include a reference to it, this is good enough. In the definition of A, we need a member of type B (not a reference), so here the entire definition of B has to be visible. Which it is, luckily.

Solving including header files in each other

It is possible to get into the situation whereby you need to include each others .h file for two classes in each others .h file and for this to cause compile errors. This will typically be for some specific private object or function. The solution is to make a forward declaration of the class the compiler has not reached yet in the .h file where you refer to the other class:

	ref class OtherClassName;
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.


Your email address will not be published. Required fields are marked *