This documentation is archived and is not being maintained.

Compiler Error C3145

Error Message

'object' : global or static variable may not have managed type 'type'

You can only define CLR objects within function scope.

The following sample generates C3145:

// C3145.cpp
// compile with: /clr
using namespace System; 
ref class G {}; 

G ^ ptr;   // C3145
G ^ ptr2 = gcnew G;   // C3145

ref class GlobalObjects {
public:
   static G ^ ptr;   // OK
   static G ^ ptr2 = gcnew G;   // OK 
}; 

int main() {
   G ^ ptr;   // OK
   G ^ ptr2 = gcnew G;   // OK
}

The following sample generates C3145:

// C3145b.cpp
// compile with: /clr
ref class MyClass {
public:
   static int data;
};

interior_ptr<int> p = &(MyClass::data);   // C3145

void Test(interior_ptr<int> x) {}

int main() {
   MyClass ^ h_MyClass = gcnew MyClass;
   interior_ptr<int> p = &(h_MyClass->data);
}

Managed Extensions for C++

The following sample generates C3145:

// C3145c.cpp
// compile with: /clr:oldSyntax
using namespace System; 
__gc class G {}; 

G *ptr;   // C3145
G *ptr2 = new G;   // C3145

__gc class GlobalObjects {
public:
   static G *ptr;   // OK
   static G *ptr2 = new G;   // OK 
}; 

int main() {
   G *ptr;   // OK
   G *ptr2 = new G;   // OK
}
Show: