Condividi tramite


Errore del compilatore C3145

'object': una variabile globale o statica non può avere il tipo gestito o WinRT 'type'

È possibile definire solo oggetti CLR o WinRT nell'ambito della funzione.

L'esempio seguente genera l'errore C3145 e mostra come risolverlo:

// 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
}

L'esempio seguente genera l'errore 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);
}