Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing GetEnumerator in C++
    primarykey
    data
    text
    <p>In C# i have the following class and it compiles just fine:</p> <p><code> class CustomItem { }</p> <pre><code>class CustomList : IList&lt;CustomItem&gt; { 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&lt;CustomItem&gt; GetEnumerator() { throw new NotImplementedException(); } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } </code></pre> <p></code></p> <p>When i try the same in C++ i get several compiler errors:</p> <p><code> ref class CustomItemValue { };</p> <pre><code>typedef CustomItemValue^ CustomItem; ref class CustomList : public IList&lt;CustomItem&gt; { public: property CustomItem default[int] { virtual CustomItem get(int index) { return nullptr; } virtual void set(int index, CustomItem value) {} } virtual void CopyTo(array&lt;CustomItem&gt;^ 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&lt;CustomItem&gt;^ GetEnumerator() { throw gcnew NotImplementedException(); } virtual System::Collections::IEnumerator^ GetEnumerator() { throw gcnew NotImplementedException(); } }; </code></pre> <p></code></p> <p>The error messages from the compiler are:</p> <p><code> .\mc.cpp(38) : error C2556: 'System::Collections::IEnumerator ^CustomList::GetEnumerator(void)' : overloaded function differs only by return type from 'System::Collections::Generic::IEnumerator ^CustomList::GetEnumerator(void)' with [ T=CustomItem ] .\mc.cpp(36) : see declaration of 'CustomList::GetEnumerator' .\mc.cpp(38) : error C2371: 'CustomList::GetEnumerator' : redefinition; different basic types .\mc.cpp(36) : see declaration of 'CustomList::GetEnumerator' </code></p> <p>Can anyone help me with this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload