2009-05-04 32 views
3

在C#我有以下的类,它编译就好:++实现的GetEnumerator用C

class CustomItem { }

class CustomList : IList<CustomItem> 
{ 
    public CustomItem this[int index] 
    { 
     get { return null; } 
     set { throw new NotImplementedException(); } 
    } 
    public void CopyTo(CustomItem[] array, int arrayIndex) 
    { 
    } 

    public int Count { get { return 10; } } 
    public int IndexOf(CustomItem item) { throw new NotImplementedException(); } 
    public void Insert(int index, CustomItem item) { throw new NotImplementedException(); } 
    public void RemoveAt(int index) { throw new NotImplementedException(); } 
    public void Add(CustomItem item) { throw new NotImplementedException(); } 
    public void Clear() { throw new NotImplementedException(); } 
    public bool Contains(CustomItem item) { throw new NotImplementedException(); } 
    public bool IsReadOnly { get { return true; } } 
    public bool Remove(CustomItem item) { throw new NotImplementedException(); } 

    public IEnumerator<CustomItem> GetEnumerator() { throw new NotImplementedException(); } 
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { throw new NotImplementedException(); } 
} 

当我尝试在C相同++我得到几个编译器错误:

ref class CustomItemValue { };

typedef CustomItemValue^ CustomItem; 

ref class CustomList : public IList<CustomItem> 
{ 
public: 
    property CustomItem default[int] 
    { 
     virtual CustomItem get(int index) { return nullptr; } 
     virtual void set(int index, CustomItem value) {} 
    } 
    virtual void CopyTo(array<CustomItem>^ array, int arrayIndex) 
    { 
    } 

    property int Count { virtual int get() { return 10; } } 
    virtual int IndexOf(CustomItem item) { throw gcnew NotImplementedException(); } 
    virtual void Insert(int index, CustomItem item) { throw gcnew NotImplementedException(); } 
    virtual void RemoveAt(int index) { throw gcnew NotImplementedException(); } 
    virtual void Add(CustomItem item) { throw gcnew NotImplementedException(); } 
    virtual void Clear() { throw new NotImplementedException(); } 
    virtual bool Contains(CustomItem item) { throw gcnew NotImplementedException(); } 
    property bool IsReadOnly { virtual bool get() { return true; } } 
    virtual bool Remove(CustomItem item) { throw gcnew NotImplementedException(); } 

    virtual IEnumerator<CustomItem>^ GetEnumerator() { throw gcnew NotImplementedException(); } 
    virtual System::Collections::IEnumerator^ GetEnumerator() 
    { throw gcnew NotImplementedException(); } 
}; 

来自编译器的错误消息是:

.\mc.cpp(38) : error C2556: 'System::Collections::IEnumerator ^CustomList::GetEnumerator(void)' : overloaded function differs only by return type from 'System::Collections::Generic::IEnumerator ^CustomList::GetEnumerator(void)' with [ T=CustomItem ] .\mc.cpp(36) : see declaration of 'CustomList::GetEnumerator' .\mc.cpp(38) : error C2371: 'CustomList::GetEnumerator' : redefinition; different basic types .\mc.cpp(36) : see declaration of 'CustomList::GetEnumerator'

任何人都可以帮助我吗?

+0

中找到更多关于显式覆盖的信息,我想知道您是否不能继承Collection ,并且只是使用覆盖来替换您想要更改的特定方法? – 2009-05-04 07:46:12

+0

对不起,但我没有在.NET中找到类Collection 。据我所知名单或阵列存在,但这不完全是我想要的。感谢您的建议,但我需要这个来访问.NET中的现有列表实现。该列表是用C++实现的,我想访问它。 – 2009-05-04 08:12:25

回答

4

您需要使用的同时重写的GetEnumerator()方法Microsoft专用明确的替代语法:

virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator 
{ throw gcnew NotImplementedException(); } 

virtual IEnumerator<CustomItem>^ GetEnumerator() 
{ throw gcnew NotImplementedException(); } 

注意,我改名非泛型GetEnumerator方法来GetEnumerator2然后指定它将覆盖系统:类别:: IEnumerable的:: GetEnumerator的。您可以在here

0

那么你有错误消息中的线索。你的GetEnumerator方法返回System :: Collections :: IEnumerator ^,而这个类的另一个方法返回 System :: Collections :: Generic :: IEnumerator ^,这个方法可能继承自IList类。

尝试用System :: Collections :: Generic :: IEnumerator ^替换返回值。

+0

对不起,但我已经试过了。 问题是编译器抱怨第二个GetEnumerator定义,但没有它,编译器会抱怨缺少的函数定义(因为GetEnumerator是抽象的)。 – 2009-05-04 08:08:38