C ++에서 GetEnumerator 구현
-
03-07-2019 - |
문제
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(); }
}
C ++에서 동일하게 시도하면 여러 컴파일러 오류가 발생합니다.
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'
누구든지 나를 도와 줄 수 있습니까?
해결책
getEnumerator () 메소드를 모두 무시하려면 Microsoft 특정 명시 적 명시 적 구문을 사용해야합니다.
virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
{ throw gcnew NotImplementedException(); }
virtual IEnumerator<CustomItem>^ GetEnumerator()
{ throw gcnew NotImplementedException(); }
GetEnumerator2로 비 게 릭 GetEnumerator 메소드의 이름을 바꾸고 System :: Collections :: Ienumerable :: getEnumerator를 무시하도록 지정합니다. 명시 적 재정의에 대해 자세히 알아볼 수 있습니다 여기
다른 팁
글쎄, 당신은 오류 메시지에 단서가 있습니다. getEnumerator 메소드는 System :: Collections :: Ienumerator^ System :: collections :: generic :: ienumerator^를 반환하는 다른 방법이 있지만이 메소드는 아마도 Ilist 클래스에서 상속 될 수 있습니다.
반환 값을 System :: Collections :: generic :: ienumerator^로 바꾸십시오.