First I know this post mentions windows forms but it isnt really a windows forms question I just use this as an example. It is a general c++ question for which I have other uses if there is a solution.
I want to declare a class to keep track of certain values. I guessed the way to do this was with static members. So for example I want to keep track of some of the windows forms in my application so I did this:
using
namespace System;using
namespace System::Windows::Forms;public
ref class Mainform : public Form{
public
:Mainform()
{
AppForms::Form1=
this;}
};
public
ref class AppForms{
public
:static
Mainform^ Form1;};
This gives the following errors:
error C2653: 'AppForms' : is not a class or namespace name
error C2065: 'Form1' : undeclared identifier
Presumably this is because AppForms is defined after I want to use it but if I swap the declarations of the classes round so AppForms is before Mainform its the same situatuon is in reverse.
How do I get round this

Static class decalarion and access
PaulDrda
Thanks very much. That solved it and was extremely helpful.
I need to extend this now so that AppForms contains statics for a number of forms.
This means the declarion of AppForms will need to be in a seperate file as its needed in the.h files of several form. h files.
I am wondering (but need to try it) if I will get more "circular/forward" reference type problems because I will need the .h file for AppForms included in my forms .h files but also the forms .h files included in the AppForms.h file.
Maybe your answer of the cpp file solves this too.
Thanks
Devo64
yes, the problem is that AppForms is defined after the reference to its type. In some cases you can try to do a forward declaration:
ref class AppForms;
This just says the compiler that there's a managed class AppForms somewhere in the current namespace.
Unfortunately this doesn't work in your case because you're using the class, i.e, you're using the Form1 field to assigned a value. Classes' forward declarations only work if you don't use the class, only make a reference to it like in method's arguements or declaring attributes/variables.
In your case you have 2 options:
1- Put the AppForms class before the MainForm and make a forward declaration of the Mainform class. (If you're using the designer for Mainform this will make the designer to produce an error since the class that will be designed needs to be the first one in the header file.)
2- Declare the Mainform constructor in the source file .cpp. This is probably the best choice:
#include "Mainform.h" // if this is the name of the header file.
Mainform::Mainfor()
{
AppForms::Form1 = this;
}
Brent Cray
forward declarations resolve the problems with circular dependencies between header files since you don't have to include the header file inside other header files (just forward declare the class), you just need to include the file in the .cpp's. Just use the second solution that I presented in the other post and everything should go well.