编译器错误 C3145
“object”:全局或静态变量不可以具有托管或 WinRT 类型“type”
仅可在函数范围内定义 CLR 或 WinRT 对象。
下面的示例生成 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
}
下面的示例生成 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);
}
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈