Domanda

In C # ho la seguente classe e si compila bene:

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

Quando provo lo stesso in C ++ ottengo diversi errori del compilatore:

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

I messaggi di errore del compilatore sono:

. \ mc.cpp (38): errore C2556: 'Sistema :: Collezioni :: IEnumerator ^ CustomList :: GetEnumerator (void)': la funzione sovraccaricata differisce solo per tipo di ritorno da 'Sistema :: Collezioni :: Generico :: IEnumerator ^ CustomList :: GetEnumerator (void)'         con         [             T = customItem         ]         . \ mc.cpp (36): vedi la dichiarazione di 'CustomList :: GetEnumerator' . \ mc.cpp (38): errore C2371: 'CustomList :: GetEnumerator': ridefinizione; diversi tipi di base         . \ mc.cpp (36): vedi la dichiarazione di 'CustomList :: GetEnumerator'

Qualcuno può aiutarmi con questo?

È stato utile?

Soluzione

È necessario utilizzare la sintassi di sostituzione esplicita specifica di Microsoft per sovrascrivere entrambi i metodi GetEnumerator ():

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

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

Nota che ho rinominato il metodo GetEnumerator non generico in GetEnumerator2, quindi specifica che sovrascrive System :: Collections :: IEnumerable :: GetEnumerator. Puoi trovare ulteriori informazioni sull'override esplicito in qui

Altri suggerimenti

Bene, hai un indizio nel messaggio di errore. il metodo GetEnumerator restituisce System :: Collections :: IEnumerator ^ mentre esiste un altro metodo per questa classe che restituisce System :: Collections :: Generic :: IEnumerator ^, questo metodo è probabilmente ereditato dalla classe IList.

Prova a sostituire il valore restituito con System :: Collections :: Generic :: IEnumerator ^.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top