在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):错误C2556:'System :: Collections :: IEnumerator ^ CustomList :: GetEnumerator(void)':重载函数的区别仅在于'System :: Collections :: Generic :: IEnumerator ^的返回类型CustomList ::的GetEnumerator(无效)”         同         [             T = CustomItem的         ]         。\ mc.cpp(36):查看'CustomList :: GetEnumerator'的声明 。\ mc.cpp(38):错误C2371:'CustomList :: GetEnumerator':重定义;不同的基本类型         。\ mc.cpp(36):查看'CustomList :: GetEnumerator'的声明

任何人都可以帮我吗?

有帮助吗?

解决方案

您需要使用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。您可以在此处中找到有关显式覆盖的更多信息

其他提示

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

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

替换返回值
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top