.NET C++/CLI – C3145, global or static variable may not have managed type

If you are trying to use a cheeky file static or global variable while eagerly developing your C++/CLI software you will probably encounter the following error:

 

“C3145, global or static variable may not have managed type”

 

At this point you remember that this isn’t really C++ but a crazy non-standard extension to C++! So instead of declaring a file static scope variable like this:


static Acquisition::IImage^ simage = nullptr;

So as a fix, you can declare a container class and add it as a static member, thus:


ref class ImageContainer {
   public:
        static Acquisition::IImage^ Image = nullptr;
};

And then access it in this way:


ImageContainer::Image = image;

Thus solution worked well for us, but of course we will be getting rid of this global variable in due time! ;)