请查看以下头文件
#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?运行这个代码,我得到很多错误
I am using a Visual C++ project (C++/CLI ?) in visual studio professional 2010. When I run this code, I get lot of errors
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的对象
Here, the namespace USBLibrary is coming from a C# dll file. The UsbHidPort; is an object from that C# dll
那么,为什么会收到这个错误?任何想法?
So, why am I getting this error? Any ideas?
推荐答案这不是一个问题在这个特定的情况下,至少从可见,但C ++ / CLI编译器试图阻止你射击你的腿,导弹风格。垃圾收集器在压缩堆时移动对象。这使得本地对象非常危险,任何指向它们的指针将变得无效,并且会在您通过它们写入时破坏GC堆。收集器无力更新这些指针,它不能找到它们。风险太高,所以编译器只是禁止它。
It isn't actually a problem in this specific case, at least from what's visible, but the C++/CLI compiler tries to prevent you from shooting your leg off, missile style. The garbage collector moves objects when it compacts the heap. Which makes native objects very dangerous, any pointers to them will become invalid and will destroy the GC heap when you write through them. The collector is powerless to update those pointers, it cannot find them. The risk is too high so the compiler just forbids it.
另一种方法是将这些成员声明为指针,并在类构造函数中使用operator new分配数组。
An alternative is to declare these members as pointers instead and allocate the array with operator new in the class constructor.
private: unsigned char* UP; // etc.. public: MissileLauncher() { UP = new unsigned char[10]; // etc.. } ~MissileLauncher() { !MissileLauncher(); } !MissileLauncher() { delete[] UP; // etc... }析构函数和终结器来释放这些数组的内存。或者只是使用正确的解决方案,使用受管阵列:
Note the hard requirement for a destructor and a finalizer to release the memory for these arrays. Or just use the sane solution, use a managed array:
private: array<Byte>^ UP; // etc.. public: MissileLauncher() { UP = gcnew array<Byte>(10); // etc.. }更多推荐
不支持混合类型
发布评论