2009-05-04 33 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