在C ++中实现GetEnumerator

时间:2009-05-04 06:56:44

标签: .net visual-c++ list c++-cli ienumerable

在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(); }
}

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(); }
};

编译器的错误消息是:

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(); } };

任何人都可以帮我吗?

2 个答案:

答案 0 :(得分:4)

您需要使用Microsoft特定的显式覆盖语法来覆盖两个GetEnumerator()方法:

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

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

请注意,我将非泛型GetEnumerator方法重命名为GetEnumerator2,然后指定它覆盖System :: Collections :: IEnumerable :: GetEnumerator。您可以在here

中找到有关显式覆盖的更多信息

答案 1 :(得分:0)

你有错误信息的线索。你的GetEnumerator方法返回System :: Collections :: IEnumerator ^,而这个类还有另一个返回的方法 System :: Collections :: Generic :: IEnumerator ^,这个方法可能是从IList类继承的。

尝试使用System :: Collections :: Generic :: IEnumerator ^。

替换返回值