请看一下下面的头文件
#pragma once
using namespace UsbLibrary;
ref class MissileLauncher
{
public:
MissileLauncher(void);
private:
//Bytes used in command
unsigned char UP[10];
unsigned char RIGHT[10];
unsigned char LEFT[10];
unsigned char DOWN[10];
unsigned char FIRE[10];
unsigned char STOP[10];
unsigned char LED_OFF[9];
unsigned char LED_ON[9];
UsbHidPort USB;
};
我正在 Visual Studio Professional 2010 中使用 Visual C++ 项目(C++/CLI?)。当我运行此代码时,出现很多错误
Error 1 error C4368: cannot define 'UP' as a member of managed 'MissileLauncher': mixed types are not supported
Error 2 error C4368: cannot define 'RIGHT' as a member of managed 'MissileLauncher': mixed types are not supported
Error 3 error C4368: cannot define 'LEFT' as a member of managed 'MissileLauncher': mixed types are not supported
Error 4 error C4368: cannot define 'DOWN' as a member of managed 'MissileLauncher': mixed types are not supported
Error 5 error C4368: cannot define 'FIRE' as a member of managed 'MissileLauncher': mixed types are not supported
Error 6 error C4368: cannot define 'STOP' as a member of managed 'MissileLauncher': mixed types are not supported
Error 7 error C4368: cannot define 'LED_OFF' as a member of managed 'MissileLauncher': mixed types are not supported
Error 8 error C4368: cannot define 'LED_ON' as a member of managed 'MissileLauncher': mixed types are not supported
这里,命名空间USBLibrary
来自 C# dll 文件。这UsbHidPort;
是来自该 C# dll 的对象
那么,为什么我会收到此错误?有任何想法吗?
在这种特定情况下,这实际上并不是一个问题,至少从可见的情况来看,但 C++/CLI 编译器会试图阻止您像导弹一样射击。垃圾收集器在压缩堆时移动对象。这使得本机对象非常危险,任何指向它们的指针都将变得无效,并且当您写入它们时会破坏 GC 堆。收集器无法更新这些指针,它无法找到它们。风险太高,所以编译器只是禁止它。
另一种方法是将这些成员声明为指针,并在类构造函数中使用运算符 new 分配数组。
private:
unsigned char* UP;
// etc..
public:
MissileLauncher() {
UP = new unsigned char[10];
// etc..
}
~MissileLauncher() {
this->!MissileLauncher();
UP = nullptr; // Destructor may be called more than once
}
!MissileLauncher() {
delete[] UP;
// etc...
}
请注意需要析构函数和终结器来释放这些数组的内存。定义析构函数还给客户端程序员带来了必须调用它的负担(Dispose() 或在 C# 客户端程序中使用,在 C++/CLI 程序中删除或堆栈语义),对于如此小的分配跳过它并不是不合理的。最后但并非最不重要的一点是,考虑合理的解决方案并使用托管数组:
private:
array<Byte>^ UP;
// etc..
public:
MissileLauncher() {
UP = gcnew array<Byte>(10);
// etc..
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)