问题描述
||
更新:如评论中所述,C ++ / CLI应该为“ 0”;编译器错误“清楚地”指出“必须是值类型”。
在C#中,我可以写
public struct Id<T> : IComparable<Id<T>>
{
public int Compareto(Id<T> other)
{
throw new NotImplementedException();
}
}
当我尝试在C ++ / CLI中执行相同操作时
generic<typename T>
public ref struct Id : System::IComparable<Id<T>>
{
public:
virtual int Compareto(Id<T> other)
{
throw gcnew System::NotImplementedException();
}
};
我收到编译器错误error C3225: generic type argument for \'T\' cannot be \'...::Id<T>\',it must be a value type or a handle to a reference type
。
这是仍然无法解决的编译器错误吗?
解决方法
在C ++ / CLI中,您需要在托管的引用类型上使用句柄。这样编译:
generic<typename T>
public ref struct Id : System::IComparable<Id<T>^>
{
public:
virtual int CompareTo(Id<T>^ other)
{
throw gcnew System::NotImplementedException();
}
};
, 如评论中所述; C#的struct
的C ++ / CLI等效值为value struct
,而不是ref struct
。