Вопрос

В C # у меня есть следующий класс, и он прекрасно компилируется:

<код>     класс 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 ++, я получаю несколько ошибок компилятора:

<код>     класс ссылки 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 (аннулируются)»         с         [             Т = 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