Does anyone have a working, step-by-step example of how to implement IEnumerable and IEnumerator in C ++ / CLI? Also, does anyone know how to fix the following code from MS Connect that does not compile in Visual Studio 2005?
http://connect.microsoft.com/VisualStudio/feedback/details/101089/how-to-implement-ienumerable-t-and-ienumerable-c-cli
using namespace System; using namespace System::Collections::Generic; generic <class T> public ref struct MyArray : public IEnumerable<T> { MyArray( array<T>^ d ) { data = d; } ref struct enumerator : IEnumerator<T> { enumerator( MyArray^ myArr ) { colInst = myArr; currentIndex = -1; } bool MoveNext() { if( currentIndex < colInst->data->Length - 1 ) { currentIndex++; return true; } return false; } property T Current { T get() { return colInst->data[currentIndex]; } };
The compiler cannot find implementations of the enumeration method:
error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'bool System::Collections::IEnumerator::MoveNext(void)' c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 55 error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'void System::Collections::IEnumerator::Reset(void)' c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 55 error C3766: 'MyArray<T>::enumerator' must provide an implementation for the interface method 'T System::Collections::Generic::IEnumerator<T>::Current::get(void)' c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 55 error C3766: 'MyArray<T>' must provide an implementation for the interface method 'System::Collections::Generic::IEnumerator<T> ^System::Collections::Generic::IEnumerable<T>::GetEnumerator(void)' c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 68
Theo
source share