C++/CLI IEnumerable和IEnumerator实现

7

有没有人有一个能够实现C++/CLI中IEnumerable和IEnumerator的工作步骤示例?或者,有人知道如何修复MS Connect中以下代码在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];
            }
        };
        // This is required as IEnumerator<T> also implements IEnumerator
        property Object^ Current2
        {
            virtual Object^ get() new sealed = System::Collections::IEnumerator::Current::get
            {
                return colInst->data[currentIndex];
            }
        };

        void Reset() {}
        ~enumerator() {}

        MyArray^ colInst;
        int currentIndex;
    };

    array<T>^ data;

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

    virtual System::Collections::IEnumerator^ GetEnumerator2() new sealed = 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;
}

编译器无法找到枚举方法的实现:
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  

尝试添加一些public:,以防万一。(是的,我知道ref struct应该默认为public)。或者使用显式override表示法,就像你对Current2::getGetEnumerator2所做的那样,让编译器告诉你为什么它没有覆盖。 - Ben Voigt
你试过Jamie说的关于将所有那些函数标记为“虚拟”的方法了吗? - Ben Voigt
1个回答

15

这在我的电脑上编译时没有任何警告(在VS2010上):

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;
}

抱歉打扰了,但是您如何将这个分离成.h和.cpp文件呢? - Benjamin
@ginkner:我不会这样做,我会像C++一样使用仅包含头文件的实现来处理模板。不过,如果你坚持,你可以在类外定义函数体,例如 generic <class T> bool MyArray<T>::enumerator::MoveNext() { ... } - Ben Voigt
如果它不是一个模板类呢? - Benjamin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接