2009-05-04 16 views
3

私は、次のクラスを持って、それがうまくコンパイル:私はC言語で同じことをしようとすると実装GetEnumeratorメソッドは、C#で

class 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(); } 
} 

++私は、いくつかのコンパイルエラーを取得:

ref class 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(); } 
}; 

コンパイラからのエラーメッセージは次のとおりです。

.\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'

誰も私にこれを手伝ってもらえますか?

+0

コレクションから継承できず、変更したい特定のメソッドにオーバーライドするだけでいいのでしょうか? –

+0

申し訳ありませんが、.NETでCollection というクラスが見つかりませんでした。私の知る限り、リストまたは配列が存在しますが、それは私が欲しいものではありません。あなたの提案をありがとうが、私はこれを.NETの既存のリスト実装にアクセスする必要があります。このリストはC++で実装されており、アクセスしたいと思います。 –

答えて

4

あなたは両方のGetEnumeratorメソッド()メソッドをオーバーライドするためのMicrosoftの特定の明示的なオーバーライドの構文を使用する必要があります。私はGetEnumerator2に非ジェネリックGetEnumeratorメソッドの名前を変更し

virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator 
{ throw gcnew NotImplementedException(); } 

virtual IEnumerator<CustomItem>^ GetEnumerator() 
{ throw gcnew NotImplementedException(); } 

注それは、システムをオーバーライドすることを指定します::コレクション:: IEnumerable :: GetEnumerator明示的なオーバーライドについてもっと知ることができますhere

+0

ありがとう、それは私が探していたものです。 –

0

エラーメッセージには手掛かりがあります。 GetEnumeratorメソッドはSystem :: Collections :: IEnumerator ^を返しますが、このクラスには、 System :: Collections :: Generic :: IEnumerator ^を返す別のメソッドがありますが、このメソッドはおそらくIListクラスから継承されています。

戻り値をSystem :: Collections :: Generic :: IEnumerator ^で置き換えてください。

+0

申し訳ありませんが、私はすでにそれを試みました。 問題は、コンパイラは2番目のGetEnumerator定義について不平を言っていますが、コンパイラは関数定義が見つからないことに不満を持ちます(GetEnumeratorが抽象であるため)。 –

関連する問題