C++ cli C++/CLI IEnumerable和IEnumerator实现

C++ cli C++/CLI IEnumerable和IEnumerator实现,c++-cli,ienumerable,ienumerator,C++ Cli,Ienumerable,Ienumerator,是否有人提供了一个关于如何在C++/CLI中实现IEnumerable和IEnumerator的实用、循序渐进的示例?或者,是否有人知道如何修复MS Connect中未在Visual Studio 2005中编译的以下代码 使用名称空间系统; 使用命名空间System::Collections::Generic; 通用的 public ref struct MyArray:public IEnumerable { MyArray(数组^d) { 数据=d; } ref结构枚举器:IEn

是否有人提供了一个关于如何在C++/CLI中实现IEnumerable和IEnumerator的实用、循序渐进的示例?或者,是否有人知道如何修复MS Connect中未在Visual Studio 2005中编译的以下代码

使用名称空间系统;
使用命名空间System::Collections::Generic;
通用的
public ref struct MyArray:public IEnumerable
{    
MyArray(数组^d)
{
数据=d;
}
ref结构枚举器:IEnumerator
{
枚举器(MyArray^myArr)
{
colInst=myArr;
currentIndex=-1;
}
bool MoveNext()
{
if(currentIndexdata->Length-1)
{
currentIndex++;
返回true;
}
返回false;
}
特性T电流
{
不明白
{
返回colInst->data[currentIndex];
}
};
//这是必需的,因为IEnumerator也实现IEnumerator
属性对象^Current2
{
虚拟对象^get()新密封=系统::集合::IEnumerator::当前::get
{
返回colInst->data[currentIndex];
}
};
void Reset(){}
~enumerator(){}
MyArray^colInst;
int-currentIndex;
};
数组^数据;
IEnumerator^GetEnumerator()
{
返回新的枚举数(this);
}
虚拟系统::集合::IEnumerator ^GetEnumerator2()新密封=系统::集合::IEnumerable::GetEnumerator
{
返回新的枚举数(this);
}
};
int main()
{
int-retval=0;
MyArray^col=gcnewmyarray(gcnewarray{10,20,30});
对于每个(col中的对象^c)
{
retval+=(int)c;
}
retval-=10+20+30;
控制台::WriteLine(“返回代码:{0}”,retval);
返回返回;
}
编译器找不到枚举器方法实现:

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  
错误C3766:“MyArray::enumerator”必须为接口方法“bool System::Collections::IEnumerator::MoveNext(void)”c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 55提供实现
错误C3766:“MyArray::enumerator”必须为接口方法“void System::Collections::IEnumerator::Reset(void)”c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 55提供实现
错误C3766:“MyArray::enumerator”必须为接口方法“T System::Collections::Generic::IEnumerator::Current::get(void)”c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 55提供实现
错误C3766:“MyArray”必须为接口方法“System::Collections::Generic::IEnumerator^System::Collections::Generic::IEnumerable::GetEnumerator(void)”c:\Projects\VCNET\2005\IEnumerable\IEnumerable\IEnumerable.cpp 68提供实现

这为我编译,没有任何警告(在VS2010上):

使用名称空间系统;
使用命名空间System::Collections::Generic;
通用的
public ref struct MyArray:public IEnumerable
{    
MyArray(数组^d)
{
数据=d;
}
ref结构枚举器:IEnumerator
{
枚举器(MyArray^myArr)
{
colInst=myArr;
currentIndex=-1;
}
虚拟布尔移动下一步()=IEnumerator::移动下一步
{
if(currentIndexdata->Length-1)
{
currentIndex++;
返回true;
}
返回false;
}
特性T电流
{
虚拟T get()=IEnumerator::Current::get
{
返回colInst->data[currentIndex];
}
};
//这是必需的,因为IEnumerator也实现IEnumerator
属性对象^Current2
{
虚拟对象^get()=System::Collections::IEnumerator::Current::get
{
返回colInst->data[currentIndex];
}
};
虚拟空重置()=IEnumerator::重置{}
~enumerator(){}
MyArray^colInst;
int-currentIndex;
};
数组^数据;
虚拟IEnumerator^GetEnumerator()
{
返回新的枚举数(this);
}
虚拟系统::集合::IEnumerator ^GetEnumerator2()=系统::集合::IEnumerable::GetEnumerator
{
返回新的枚举数(this);
}
};
int main()
{
int-retval=0;
MyArray^col=gcnewmyarray(gcnewarray{10,20,30});
对于每个(col中的对象^c)
{
retval+=(int)c;
}
retval-=10+20+30;
控制台::WriteLine(“返回代码:{0}”,retval);
返回返回;
}

尝试在一些
公共场所喷洒:
以防万一。(是的,我知道
ref-struct
应该默认为public)。或者使用显式重写表示法,就像你对
Current2::get
GetEnumerator2
所做的那样,让编译器告诉你为什么它没有重写。你有没有试过Jamie所说的把所有那些函数标记为
virtual
,我只会使用一个头表单实现,就像C++总是为模板所做的那样。但是,如果您坚持,您可以在类之外定义函数体,比如
generic bool MyArray::enumerator::MoveNext(){…}
如果它不是模板类呢?
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  
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;
        }

        virtual bool MoveNext() = IEnumerator<T>::MoveNext
        {
            if( currentIndex < colInst->data->Length - 1 )
            {
                currentIndex++;
                return true;
            }
            return false;
        }

        property T Current
        {
            virtual T get() = IEnumerator<T>::Current::get
            {
                return colInst->data[currentIndex];
            }
        };
        // This is required as IEnumerator<T> also implements IEnumerator
        property Object^ Current2
        {
            virtual Object^ get() = System::Collections::IEnumerator::Current::get
            {
                return colInst->data[currentIndex];
            }
        };

        virtual void Reset() = IEnumerator<T>::Reset {}
        ~enumerator() {}

        MyArray^ colInst;
        int currentIndex;
    };

    array<T>^ data;

    virtual IEnumerator<T>^ GetEnumerator()
    {
        return gcnew enumerator(this);
    }

    virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
    {
        return gcnew enumerator(this);
    }
};

int main()
{
    int retval = 0;

    MyArray<int>^ col = gcnew MyArray<int>( gcnew array<int>{10, 20, 30 } );

    for each( Object^ c in col )
    {
        retval += (int)c;
    }
    retval -= 10 + 20 + 30;

    Console::WriteLine("Return Code: {0}", retval );
    return retval;
}