Visual c++ C++;在本机代码中声明托管变量
我在VisualStudio中有一个.NET表单和一个本机代码。问题是:我无法在本机代码中声明.NET表单的全局实例,如下所示:Visual c++ C++;在本机代码中声明托管变量,visual-c++,variables,native,global,managed,Visual C++,Variables,Native,Global,Managed,我在VisualStudio中有一个.NET表单和一个本机代码。问题是:我无法在本机代码中声明.NET表单的全局实例,如下所示: Editor^ maineditor; 这给了我一个问题: error C3145: 'EditorEntry' : global or static variable may not have managed type 'Cube3D::Editor ^' 不要使用全局静态方法,而是尝试将其作为容器类型中的静态方法 ref class ManagedGlobal
Editor^ maineditor;
这给了我一个问题:
error C3145: 'EditorEntry' : global or static variable may not have managed type 'Cube3D::Editor ^'
不要使用全局静态方法,而是尝试将其作为容器类型中的静态方法
ref class ManagedGlobals {
public:
static Editor^ maineditor = nullptr;
};
用gcroot结构包装句柄
gcroot主编辑器;
您的静态类位于顶部(参考:)
现在只需引用该类
ManagedGlobals::xl = gcnew Excel::Application();
C3145的MSDN文章很好地记录了这个错误。也给出了解决方法,使其成为ref类的静态成员。谢谢,但是当我定义编辑器时,第二个代码出现了:具有静态存储持续时间的变量不能有句柄或跟踪引用类型,并且在编译时:“editor”:全局或静态变量可能没有托管类型“Cube3D::editor^”@user1492812 oops,我不知道这是个问题。删除了答案中的这一部分,我为误解感到抱歉。EditorEntry是我的编辑器实例的前一个名称,很抱歉:/比创建一个虚拟包装器类更有趣、更干净:-)
ref class ManagedGlobals abstract sealed {
public:
static Excel::Application^ xl;
};
ManagedGlobals::xl = gcnew Excel::Application();