2016-04-26 113 views
3

我正在嘗試使用TreeNodes在代碼中創建樹狀結構。我根本不熟悉TreeNodes。我事先做了一些搜索,但我仍然不覺得我完全明白我在做什麼。TreeNode創建錯誤

我使用C#在Unity中創建遊戲。我使用XML來創建對話框,並且我想將來自不同選項的不同選項存儲在樹狀結構中。

這將是這樣的視覺表示:

------------------------------- choice a ---------------------------選擇b --------------------- -----------

   /--------|--------\   /--------|--------\ 

      choice d choice e choice f choice g choice h choice i 

等等。

public class TreeNode<T> : IEnumerable<TreeNode<T>> 
{ 
    public T Data {get; set;} 
    public TreeNode<T> Parent { get; set; } 
    public ICollection<TreeNode<T>> Children {get; set;} 

    public TreeNode(T data) { 
     this.Data = data; 
     this.Children = new LinkedList<TreeNode<T>>(); 
    } 

    public TreeNode<T> AddChild(T child) { 
     TreeNode<T> childNode = new TreeNode<T>(child) {Parent = this}; 
     this.Children.Add (childNode); 
     return childNode; 
    } 
} 

目前我得到的錯誤TreeNode<T>' does not implement interface member System.Collections.Generic.IEnumerable> .GetEnumerator()」。

我不完全確定這個錯誤甚至意味着什麼,任何幫助將不勝感激。

我第一次在StackOverFlow中提出問題,所以如果這是錯誤的地方,請讓我知道。

感謝

+0

是你能夠解決您的問題? – gnalck

+0

是的!謝謝您的幫助。 – Carbonroast

回答

0

的問題是在你宣佈TreeNode<T>(會)執行IEnumerable<TreeNode<T>>行。爲了正確地實現該接口,該類需要實現接口公開的所有方法。

public class TreeNode<T> : IEnumerable<TreeNode<T>> 

刪除: IEnumerable<TreeNode<T>>它會正確編譯。除非你想要實現這個接口,否則你需要添加所有IEnumerable方法的實現。

0

您可以嘗試刪除繼承的: IEnumerable<TreeNode<T>>,或者您需要實施System.Collections.IEnumerable.GetEnumerator()方法。

請參閱this線程對錯誤進行一些額外的閱讀,並可能進一步瞭解。

引用 「你沒有實現System.Collections.IEnumerable.GetEnumerator。 當你實現了通用的IEnumerable,你也必須實現System.Collections.IEnumerable的GetEnumerator。