Implémentation de GetEnumerator en C ++
-
03-07-2019 - |
Question
En C #, j'ai la classe suivante et la compilation est parfaite:
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(); }
}
Quand j'essaie la même chose en C ++, je reçois plusieurs erreurs de compilation:
classe de référence 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(); }
};
Les messages d'erreur du compilateur sont les suivants:
. \ mc.cpp (38): erreur C2556: 'Système :: Collections :: IEnumerator ^ CustomList :: GetEnumerator (void)': la fonction surchargée ne diffère que par le type de retour de 'System :: Collections :: Generic :: IEnumerator ^ CustomList :: GetEnumerator (void) '
avec
[
T = CustomItem
]
. \ mc.cpp (36): voir la déclaration de 'CustomList :: GetEnumerator'
. \ mc.cpp (38): erreur C2371: 'CustomList :: GetEnumerator': redefinition; différents types de base
. \ mc.cpp (36): voir la déclaration de 'CustomList :: GetEnumerator'
Quelqu'un peut-il m'aider avec cela?
La solution
Vous devez utiliser la syntaxe de substitution explicite spécifique à Microsoft pour redéfinir les deux méthodes GetEnumerator ():
virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
{ throw gcnew NotImplementedException(); }
virtual IEnumerator<CustomItem>^ GetEnumerator()
{ throw gcnew NotImplementedException(); }
Notez que j'ai renommé la méthode GetEnumerator non générique en GetEnumerator2, puis spécifie qu'elle substitue System :: Collections :: IEnumerable :: GetEnumerator. Pour en savoir plus sur le remplacement explicite, consultez ici
.Autres conseils
Eh bien, vous avez un indice dans le message d’erreur. votre méthode GetEnumerator renvoie System :: Collections :: IEnumerator ^ alors qu'une autre méthode renvoie cette classe System :: Collections :: Generic :: IEnumerator ^, cette méthode est probablement héritée de la classe IList.
Essayez de remplacer la valeur de retour par System :: Collections :: Generic :: IEnumerator ^.